Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_load_qasm_text(self):
"""Test load_qasm_text and get_circuit.
If all is correct we should get the qasm file loaded from the string
Previusly:
Libraries:
from qiskit import QuantumProgram
"""
QP_program = QuantumProgram()
QASM_string = "// A simple 8 qubit example\nOPENQASM 2.0;\n"
QASM_string += "include \"qelib1.inc\";\nqreg a[4];\n"
QASM_string += "qreg b[4];\ncreg c[4];\ncreg d[4];\nh a;\ncx a, b;\n"
QASM_string += "barrier a;\nbarrier b;\nmeasure a[0]->c[0];\n"
QASM_string += "measure a[1]->c[1];\nmeasure a[2]->c[2];\n"
QASM_string += "measure a[3]->c[3];\nmeasure b[0]->d[0];\n"
QASM_string += "measure b[1]->d[1];\nmeasure b[2]->d[2];\n"
QASM_string += "measure b[3]->d[3];"
name = QP_program.load_qasm_text(QASM_string, verbose=False)
result = QP_program.get_circuit(name)
to_check = result.qasm()
# print(to_check)
self.assertEqual(len(to_check), 554)
def test_create_circuit(self):
"""Test create_circuit.
If all is correct we get a object instance of QuantumCircuit
Previously:
Libraries:
from qiskit import QuantumProgram
from qiskit import QuantumCircuit
"""
q_program = QuantumProgram()
qr = q_program.create_quantum_register("qr", 3)
cr = q_program.create_classical_register("cr", 3)
qc = q_program.create_circuit("qc", [qr], [cr])
self.assertIsInstance(qc, QuantumCircuit)
def test_run_program(self):
"""Test run.
If all correct should the data.
"""
q_program = QuantumProgram(specs=self.QPS_SPECS)
qr = q_program.get_quantum_register("q_name")
cr = q_program.get_classical_register("c_name")
qc2 = q_program.create_circuit("qc2", [qr], [cr])
qc3 = q_program.create_circuit("qc3", [qr], [cr])
qc2.h(qr[0])
qc2.cx(qr[0], qr[1])
qc2.cx(qr[0], qr[2])
qc3.h(qr)
qc2.measure(qr, cr)
qc3.measure(qr, cr)
circuits = ['qc2', 'qc3']
shots = 1024
backend = 'local_qasm_simulator'
qobj = q_program.compile(circuits, backend=backend, shots=shots,
seed=88)
result = q_program.run(qobj)
def test_local_unitary_simulator(self):
"""Test unitary simulator.
If all correct should the hxh and cx.
"""
q_program = QuantumProgram()
q = q_program.create_quantum_register("q", 2)
c = q_program.create_classical_register("c", 2)
qc1 = q_program.create_circuit("qc1", [q], [c])
qc2 = q_program.create_circuit("qc2", [q], [c])
qc1.h(q)
qc2.cx(q[0], q[1])
circuits = ['qc1', 'qc2']
backend = 'local_unitary_simulator'
result = q_program.execute(circuits, backend=backend)
unitary1 = result.get_data('qc1')['unitary']
unitary2 = result.get_data('qc2')['unitary']
unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5],
[0.5, 0.5, -0.5, -0.5],
[0.5, -0.5, -0.5, 0.5]])
unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1],
[0., 0, 1, 0], [0, 1, 0, 0]])
def _get_quantum_program():
quantum_program = QuantumProgram()
qr = quantum_program.create_quantum_register("q", 1)
cr = quantum_program.create_classical_register("c", 1)
qc = quantum_program.create_circuit("qc", [qr], [cr])
qc.h(qr[0])
qc.measure(qr[0], cr[0])
return quantum_program
def test_average_data(self):
"""Test average_data.
If all correct should the data.
"""
QP_program = QuantumProgram()
q = QP_program.create_quantum_register("q", 2, verbose=False)
c = QP_program.create_classical_register("c", 2, verbose=False)
qc = QP_program.create_circuit("qc", [q], [c])
qc.h(q[0])
qc.cx(q[0], q[1])
qc.measure(q[0], c[0])
qc.measure(q[1], c[1])
circuits = ['qc']
shots = 10000 # the number of shots in the experiment.
backend = 'local_qasm_simulator'
results = QP_program.execute(circuits, backend=backend, shots=shots)
observable = {"00": 1, "11": 1, "01": -1, "10": -1}
meanzz = results.average_data("qc", observable)
observable = {"00": 1, "11": -1, "01": 1, "10": -1}
meanzi = results.average_data("qc", observable)
observable = {"00": 1, "11": -1, "01": -1, "10": 1}
def test_combine_results(self):
"""Test run.
If all correct should the data.
"""
q_program = QuantumProgram()
qr = q_program.create_quantum_register("qr", 1)
cr = q_program.create_classical_register("cr", 1)
qc1 = q_program.create_circuit("qc1", [qr], [cr])
qc2 = q_program.create_circuit("qc2", [qr], [cr])
qc1.measure(qr[0], cr[0])
qc2.x(qr[0])
qc2.measure(qr[0], cr[0])
shots = 1024
backend = 'local_qasm_simulator'
res1 = q_program.execute(['qc1'], backend=backend, shots=shots)
res2 = q_program.execute(['qc2'], backend=backend, shots=shots)
counts1 = res1.get_counts('qc1')
counts2 = res2.get_counts('qc2')
res1 += res2 # combine results
counts12 = [res1.get_counts('qc1'), res1.get_counts('qc2')]
self.assertEqual(counts12, [counts1, counts2])
def test_execute_one_circuit_real_online(self):
QP_program = QuantumProgram(specs=QPSpecs)
qc, qr, cr = QP_program.quantum_elements()
qc.h(qr[1])
device = 'qx5q' # the device to run on
shots = 1 #the number of shots in the experiment.
apiconnection = QP_program.set_api(Qconfig.APItoken, Qconfig.config["url"])
result = QP_program.run_circuit("circuit-name", device, shots, max_credits=3)
self.assertEqual(result["status"], "DONE")
def test_load_wrong(self):
"""
Load a Json Quantum Program: Errors Control
"""
QP_program = QuantumProgram(specs=QPS_SPECS)
self.assertRaises(LookupError, QP_program.load)
def eval_cost_function(entangler_map, coupling_map, initial_layout, n, m, x_vec, class_labels,
backend, shots, seed, train, theta):
sample_shots = 0
# x_vec is the vector of training characteristics - size n
# y is the binary outcome for each x_vec
number_of_classes = len(class_labels)
cost = 0
total_cost = 0
std_cost = 0
predicted_results = []
Q_program = QuantumProgram()
# Q_program.set_api(Qconfig.APItoken,Qconfig.config["url"])
### RUN CIRCUITS
circuits = []
c = []
sequences = []
unlabeled_data = []
for arrays in range(number_of_classes):
labelgroup = x_vec[class_labels[arrays]]
for item in labelgroup:
unlabeled_data.append(item)
# unlabeled_data = np.array([])
# for arrays in range(number_of_classes):
# unlabeled_data = np.vstack((unlabeled_data,x_vec[class_labels[arrays]])) if unlabeled_data.size else x_vec[class_labels[arrays]]