Skip to content

Commit

Permalink
added ref_initialize_temp files to test_statevector_simulator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ShellyGarion committed Apr 10, 2019
1 parent b0d5ada commit f66208a
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 3 deletions.
8 changes: 5 additions & 3 deletions test/terra/test_statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from test.terra.utils import ref_measure
from test.terra.utils import ref_reset
from test.terra.utils import ref_initialize
from test.terra.utils import ref_initialize_temp_1
from test.terra.utils import ref_initialize_temp_2
from test.terra.utils import ref_conditionals
from test.terra.utils import ref_1q_clifford
from test.terra.utils import ref_2q_clifford
Expand All @@ -38,11 +40,11 @@ def test_initialize_deterministic(self):
targets = ref_initialize.initialize_statevector_deterministic()
job = execute(circuits, StatevectorSimulator(), shots=1)
result = job.result()
print (result)
#statevector = result.get_statevector(circuits)
#print (statevector)
#print (result)
# self.is_completed(result)
# self.compare_statevector(result, circuits, targets)
ref_initialize_temp_1.initialize_qobj_direct()
ref_initialize_temp_2.initialize_QuantumCircuit()

# ---------------------------------------------------------------------
# Test reset
Expand Down
243 changes: 243 additions & 0 deletions test/terra/utils/ref_initialize_temp_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import numpy as np
import pprint
import qiskit
from qiskit import Aer, execute
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, QiskitError
from qiskit.providers.aer import StatevectorSimulator
from qiskit.qobj import QasmQobjInstruction

def initialize_qobj_direct():

print("Initializing Qobj by adding QasmQobjInstruction:")
# Construct an quantum circuit - |+++>
qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circ = QuantumCircuit(qr, cr)
circ.h(qr[0])
circ.h(qr[1])
circ.h(qr[2])

print ("start with |+++> state: ")

print (circ)

# Select the QasmSimulator from the Aer provider
simulator = Aer.get_backend('statevector_simulator')

# Execute and get counts
#result = execute(circ, simulator).result()
qobj = qiskit.compile(circ, backend=simulator)
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

#Execute after reset&initialize - 1 qubit
for qubit in range(3):
print ("--------------------------------------------------------")
print ("--------------------------------------------------------")
print ("reset: qubit", qubit)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='reset', qubits=[qubit]))
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

print("--------------------------------------------------------")
print ("initialize: qubit", qubit)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit], params=[[0,0],[1,0]]))
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)


#Execute after reset&initialize - 2 qubits
for qubit_i in range(3):
for qubit_j in range(3):
if (qubit_i != qubit_j):
print ("--------------------------------------------------------")
print ("--------------------------------------------------------")
print ("reset: qubits i, j: ", qubit_i, qubit_j)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='reset', qubits=[qubit_i, qubit_j]))
print (qobj)
result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

print("--------------------------------------------------------")
print("plain initialize: qubits i, j: ", qubit_i, qubit_j)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_i], params=[[0, 0],[1, 0]]))
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_j], params=[[1, 0],[0, 0]]))
print(qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print("statevector:", statevector)
print("counts:", counts)

plain_statevector = statevector

print ("--------------------------------------------------------")
print ("initialize: qubits i, j: ", qubit_i, qubit_j)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_i, qubit_j],\
params=[[0, 0], [1, 0], [0, 0], [0, 0]]))
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

print(statevector == plain_statevector)

#Execute after reset&initialize - 3 qubits
for qubit_i in range(3):
for qubit_j in range(3):
for qubit_k in range(3):
if ((qubit_i != qubit_j) & (qubit_i != qubit_k) & (qubit_k != qubit_j)):
print ("--------------------------------------------------------")
print ("--------------------------------------------------------")
print ("reset: qubits i, j, k: ", qubit_i, qubit_j, qubit_k)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='reset', qubits=[qubit_i, qubit_j, qubit_k]))
print (qobj)
result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

print ("--------------------------------------------------------")
print ("plain initialize: qubits i, j, k: ", qubit_i, qubit_j, qubit_k)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_i], params=[[0, 0], [1, 0]]))
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_j], params=[[1, 0], [0, 0]]))
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[qubit_k], params=[[0.70710678, 0], [-0.70710678, 0]]))
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

plain_statevector = statevector
print ("statevector:", statevector)
print ("counts:", counts)

print("--------------------------------------------------------")
print("initialize: qubits i, j, k: ", qubit_i, qubit_j, qubit_k)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(
QasmQobjInstruction(name='initialize', qubits=[qubit_i,qubit_j,qubit_k],
params=[[0, 0], [0.70710678, 0], [0, 0], [0, 0],
[0, 0], [-0.70710678, 0], [0, 0], [0, 0]]))
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print("statevector:", statevector)
print("counts:", counts)

print (statevector == plain_statevector)

print ("-------------------------------------------")
print ("-------------------------------------------")
print ("-------------------------------------------")
print ("start with Bell state: ")

# Construct a quantum circuit - Bell
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circ = QuantumCircuit(qr, cr)
circ.h(qr[0])
circ.cx(qr[0], qr[1])

print (circ)

# Select the QasmSimulator from the Aer provider
simulator = Aer.get_backend('statevector_simulator')

# Execute and get counts
#result = execute(circ, simulator).result()
qobj = qiskit.compile(circ, backend=simulator)
print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print ("statevector:", statevector)
print ("counts:", counts)

print("--------------------------------------------------------")
print("--------------------------------------------------------")
print("reset: qubit", 0)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='reset', qubits=[0]))
# print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print("statevector:", statevector)
print("counts:", counts)

print("--------------------------------------------------------")
print("--------------------------------------------------------")

for times in range(20):
print("initialize: qubit", 0)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[0], params=[[0.70710678, 0], [0.70710678, 0]]))
# print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print("statevector:", statevector)
#print("counts:", counts)

print("--------------------------------------------------------")
print("--------------------------------------------------------")

for times in range(20):
print("initialize: qubit", 0)
qobj = qiskit.compile(circ, backend=simulator)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[0], params=[[0.70710678, 0], [-0.70710678, 0]]))
# print (qobj)

result = simulator.run(qobj).result()
statevector = result.get_statevector(circ)
counts = result.get_counts(circ)

print("statevector:", statevector)
#print("counts:", counts)
123 changes: 123 additions & 0 deletions test/terra/utils/ref_initialize_temp_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from qiskit import *
from qiskit import Aer, execute
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, QiskitError
from qiskit.providers.aer import StatevectorSimulator
from qiskit.compiler import assemble_circuits
from qiskit.qobj import QasmQobjInstruction
import numpy as np

def initialize_QuantumCircuit():
print("--------------------------------------")
print("--------------------------------------")
print("--------------------------------------")
print("Initializing QuantumCircuit:")
qr = QuantumRegister(2)

# Select the QasmSimulator from the Aer provider
simulator = Aer.get_backend('statevector_simulator')

# Method 1
circ1 = QuantumCircuit(qr)
circ1.initialize([0, 0, 0, 1], qr[:])
print(circ1)

# Execute and get counts
result = execute(circ1, simulator).result()
qobj = assemble_circuits(circ1)
print(qobj)
statevector = result.get_statevector(circ1)
print ("statevector of circ1:", statevector)

# Method 2
circ2 = QuantumCircuit(qr)
circ2.initialize([0, 0, 0, 1], [qr[0], qr[1]])
print(circ2)

# Execute and get counts
result = execute(circ2, simulator).result()
statevector = result.get_statevector(circ2)
print ("statevector of circ2:", statevector)

# Method 3
circ3 = QuantumCircuit(qr)
circ3.initialize([0, 1], [qr[0]])
circ3.initialize([0, 1], [qr[1]])
print(circ3)

# Execute and get counts
result = execute(circ3, simulator).result()
qobj = assemble_circuits(circ3)
print(qobj)
statevector = result.get_statevector(circ3)
print ("statevector of circ3:", statevector)

# Implementation
circ0 = QuantumCircuit(qr)
circ0.reset(qr[0])
circ0.x(qr[0])
circ0.reset(qr[1])
circ0.x(qr[1])
print(circ0)

# Execute and get counts
result = execute(circ0, simulator).result()
statevector = result.get_statevector(circ0)
print ("statevector of circ0:", statevector)

# assemble into a qobj
qobj = assemble_circuits([circ0, circ1, circ2, circ3])
print ("*******************************")
print (qobj)
print ("*******************************")

qr = QuantumRegister(3)

circ4 = QuantumCircuit(qr)
circ4.h(qr[0])
circ4.h(qr[1])
circ4.h(qr[2])
print(circ4)

# Execute and get counts
result = execute(circ4, simulator).result()
statevector = result.get_statevector(circ4)
print ("statevector of circ4:", statevector)

qobj = assemble_circuits(circ4)
print(qobj)
qobj.experiments[0].instructions.append(QasmQobjInstruction(name='initialize', qubits=[0], params=[[0,0],[1,0]]))
print(qobj)

#result = simulator.run(qobj).result()
#statevector = result.get_statevector(circ4)
#print ("statevector of updated circ4:", statevector)

circ5 = QuantumCircuit(qr)
circ5.h(qr[0])
circ5.h(qr[1])
circ5.h(qr[2])
circ5.reset (qr[0])
print(circ5)

# Execute and get counts
result = execute(circ5, simulator).result()
qobj = assemble_circuits(circ5)
print(qobj)
statevector = result.get_statevector(circ5)
print ("statevector of circ5:", statevector)


circ6 = QuantumCircuit(qr)
circ6.h(qr[0])
circ6.h(qr[1])
circ6.h(qr[2])
#circ6.initialize(np.array([0,1], dtype=complex), [qr[0]])
circ6.initialize(np.array([0.+0.j, 1.+0.j]), [qr[0]])
print(circ6)

# Execute and get counts
result = execute(circ6, simulator).result()
qobj = assemble_circuits(circ6)
print(qobj)
statevector = result.get_statevector(circ6)
print ("statevector of circ6:", statevector)

0 comments on commit f66208a

Please sign in to comment.