def test_qasm_snapshot(self): """snapshot a circuit at multiple places""" qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.snapshot(1) circuit.ccx(qr[0], qr[1], qr[2]) circuit.snapshot(2) circuit.reset(qr) circuit.snapshot(3) circuit.measure(qr, cr) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' result_cpp = execute(circuit, sim_cpp, shots=2).result() result_py = execute(circuit, sim_py, shots=2).result() snapshots_cpp = result_cpp.get_snapshots() snapshots_py = result_py.get_snapshots() self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys()) snapshot_cpp_1 = result_cpp.get_snapshot(slot='1') snapshot_py_1 = result_py.get_snapshot(slot='1') self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1)) fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0]) self.assertGreater(fidelity, self._desired_fidelity)
def test_basic_reordering(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """a simple reordering within a 2-qubit register""" sim, real = self._get_backends(QE_TOKEN, QE_URL, hub, group, project) if not sim or not real: raise unittest.SkipTest('no remote device available') q = qiskit.QuantumRegister(2) c = qiskit.ClassicalRegister(2) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.measure(q[0], c[1]) circ.measure(q[1], c[0]) shots = 2000 result_real = execute(circ, real, {"shots": shots}).result(timeout=600) result_sim = execute(circ, sim, {"shots": shots}).result() counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() self.log.info(counts_real) self.log.info(counts_sim) threshold = 0.1 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_random_circuits(self): qk_simulator = get_backend('local_qasm_simulator') for circuit in self.rqg.get_circuits(format_='QuantumCircuit'): self.log.info(circuit.qasm()) shots = 1000 min_cnts = int(shots / 10) result_pq = execute(circuit, pq_simulator.name) result_qk = execute(circuit, qk_simulator.name) counts_pq = result_pq.get_counts(result_pq.get_names()[0]) counts_qk = result_qk.get_counts(result_qk.get_names()[0]) # filter states with few counts counts_pq = { key: cnt for key, cnt in counts_pq.items() if cnt > min_cnts } counts_qk = { key: cnt for key, cnt in counts_qk.items() if cnt > min_cnts } self.log.info('local_qasm_simulator_projectq: %s', str(counts_pq)) self.log.info('local_qasm_simulator: %s', str(counts_qk)) threshold = 0.05 * shots self.assertDictAlmostEqual(counts_pq, counts_qk, threshold) states = counts_qk.keys() # contingency table ctable = numpy.array([[counts_pq[key] for key in states], [counts_qk[key] for key in states]]) result = chi2_contingency(ctable) self.log.info('chi2_contingency: %s', str(result)) with self.subTest(circuit=circuit): self.assertGreater(result[1], 0.01)
def test_qasm_snapshot(self): """snapshot a circuit at multiple places""" q = qk.QuantumRegister(3) c = qk.ClassicalRegister(3) circ = qk.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.snapshot(1) circ.ccx(q[0], q[1], q[2]) circ.snapshot(2) circ.reset(q) circ.snapshot(3) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' result_cpp = execute(circ, sim_cpp, {'shots': 2}) result_py = execute(circ, sim_py, {'shots': 2}) snapshots_cpp = result_cpp.get_snapshots() snapshots_py = result_py.get_snapshots() self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys()) snapshot_cpp_1 = result_cpp.get_snapshot(slot='1') snapshot_py_1 = result_py.get_snapshot(slot='1') self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1)) fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0]) self.assertGreater(fidelity, self._desired_fidelity)
def test_multi_register_reordering(self, QE_TOKEN, QE_URL): """a more complicated reordering across 3 registers of different sizes""" sim, real = self._get_backends(QE_TOKEN, QE_URL) if not sim or not real: raise unittest.SkipTest('no remote device available') q0 = qiskit.QuantumRegister(2) q1 = qiskit.QuantumRegister(2) q2 = qiskit.QuantumRegister(1) c0 = qiskit.ClassicalRegister(2) c1 = qiskit.ClassicalRegister(2) c2 = qiskit.ClassicalRegister(1) circ = qiskit.QuantumCircuit(q0, q1, q2, c0, c1, c2) circ.h(q0[0]) circ.cx(q0[0], q2[0]) circ.x(q1[1]) circ.h(q2[0]) circ.ccx(q2[0], q1[1], q1[0]) circ.barrier() circ.measure(q0[0], c2[0]) circ.measure(q0[1], c0[1]) circ.measure(q1[0], c0[0]) circ.measure(q1[1], c1[0]) circ.measure(q2[0], c1[1]) shots = 4000 result_real = execute(circ, real, {"shots": shots}).result(timeout=600) result_sim = execute(circ, sim, {"shots": shots}).result() counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() threshold = 0.2 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_qasm_snapshot(self): """snapshot a circuit at multiple places""" q = qiskit.QuantumRegister(3) c = qiskit.ClassicalRegister(3) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.snapshot(1) circ.ccx(q[0], q[1], q[2]) circ.snapshot(2) circ.reset(q) circ.snapshot(3) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' result_cpp = execute(circ, sim_cpp, shots=2).result() result_py = execute(circ, sim_py, shots=2).result() snapshots_cpp = result_cpp.get_snapshots() snapshots_py = result_py.get_snapshots() self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys()) snapshot_cpp_1 = result_cpp.get_snapshot(slot='1') snapshot_py_1 = result_py.get_snapshot(slot='1') self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1)) fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0]) self.assertGreater(fidelity, self._desired_fidelity)
def test_qasm_reset_measure(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """counts from a qasm program with measure and reset in the middle""" register(QE_TOKEN, QE_URL, hub, group, project) q = qiskit.QuantumRegister(3) c = qiskit.ClassicalRegister(3) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.reset(q[0]) circ.cx(q[1], q[2]) circ.t(q) circ.measure(q[1], c[1]) circ.h(q[2]) circ.measure(q[2], c[2]) # TODO: bring back online simulator tests when reset/measure doesn't # get rejected by the api sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' # sim_ibmq = 'ibmq_qasm_simulator' shots = 1000 result_cpp = execute(circ, sim_cpp, shots=shots, seed=1).result() result_py = execute(circ, sim_py, shots=shots, seed=1).result() # result_ibmq = execute(circ, sim_ibmq, {'shots': shots}).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() # counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.04)
def test_multi_register_reordering(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """a more complicated reordering across 3 registers of different sizes""" sim, real = self._get_backends(QE_TOKEN, QE_URL, hub, group, project) if not sim or not real: raise unittest.SkipTest('no remote device available') q0 = qiskit.QuantumRegister(2) q1 = qiskit.QuantumRegister(2) q2 = qiskit.QuantumRegister(1) c0 = qiskit.ClassicalRegister(2) c1 = qiskit.ClassicalRegister(2) c2 = qiskit.ClassicalRegister(1) circ = qiskit.QuantumCircuit(q0, q1, q2, c0, c1, c2) circ.h(q0[0]) circ.cx(q0[0], q2[0]) circ.x(q1[1]) circ.h(q2[0]) circ.ccx(q2[0], q1[1], q1[0]) circ.barrier() circ.measure(q0[0], c2[0]) circ.measure(q0[1], c0[1]) circ.measure(q1[0], c0[0]) circ.measure(q1[1], c1[0]) circ.measure(q2[0], c1[1]) shots = 4000 result_real = execute(circ, real, {"shots": shots}).result(timeout=600) result_sim = execute(circ, sim, {"shots": shots}).result() counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() threshold = 0.2 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_qasm_reset_measure(self, qe_token, qe_url): """counts from a qasm program with measure and reset in the middle""" qiskit.IBMQ.use_account(qe_token, qe_url) qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.reset(qr[0]) circuit.cx(qr[1], qr[2]) circuit.t(qr) circuit.measure(qr[1], cr[1]) circuit.h(qr[2]) circuit.measure(qr[2], cr[2]) # TODO: bring back online simulator tests when reset/measure doesn't # get rejected by the api sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' # sim_ibmq = 'ibmq_qasm_simulator' shots = 1000 result_cpp = execute(circuit, sim_cpp, shots=shots, seed=1).result() result_py = execute(circuit, sim_py, shots=shots, seed=1).result() # result_ibmq = execute(circ, sim_ibmq, {'shots': shots}).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() # counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.04)
def test_qasm_reset_measure(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """counts from a qasm program with measure and reset in the middle""" register(QE_TOKEN, QE_URL, hub, group, project) q = qiskit.QuantumRegister(3) c = qiskit.ClassicalRegister(3) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.reset(q[0]) circ.cx(q[1], q[2]) circ.t(q) circ.measure(q[1], c[1]) circ.h(q[2]) circ.measure(q[2], c[2]) # TODO: bring back online simulator tests when reset/measure doesn't # get rejected by the api sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' # sim_ibmq = 'ibmq_qasm_simulator' shots = 1000 result_cpp = execute(circ, sim_cpp, shots=shots, seed=1).result() result_py = execute(circ, sim_py, shots=shots, seed=1).result() # result_ibmq = execute(circ, sim_ibmq, {'shots': shots}).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() # counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.04)
def test_qasm(self, QE_TOKEN, QE_URL): """counts from a GHZ state""" register(QE_TOKEN, QE_URL) q = qk.QuantumRegister(3) c = qk.ClassicalRegister(3) circ = qk.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.cx(q[1], q[2]) circ.measure(q, c) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' sim_ibmq = 'ibmq_qasm_simulator' sim_hpc = 'ibmq_qasm_simulator_hpc' shots = 2000 result_cpp = execute(circ, sim_cpp, {'shots': shots}) result_py = execute(circ, sim_py, {'shots': shots}) result_ibmq = execute(circ, sim_ibmq, {'shots': shots}) result_hpc = execute(circ, sim_hpc, {'shots': shots}) counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() counts_ibmq = result_ibmq.get_counts() counts_hpc = result_hpc.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.025) self.assertDictAlmostEqual(counts_py, counts_ibmq, shots * 0.025) self.assertDictAlmostEqual(counts_ibmq, counts_hpc, shots * 0.025)
def setUp(self): qr = qiskit.QuantumRegister(1) cr = qiskit.ClassicalRegister(1) self._qc1 = qiskit.QuantumCircuit(qr, cr, name='qc1') self._qc2 = qiskit.QuantumCircuit(qr, cr, name='qc2') self._qc1.measure(qr[0], cr[0]) self._qc2.x(qr[0]) self._qc2.measure(qr[0], cr[0]) self.backend = 'local_qasm_simulator' self._result1 = execute(self._qc1, self.backend).result() self._result2 = execute(self._qc2, self.backend).result()
def test_sympy(self): desired_vector = [ 0, math.cos(math.pi / 3) * complex(0, 1) / math.sqrt(4), math.sin(math.pi / 3) / math.sqrt(4), 0, 0, 0, 0, 0, 1 / math.sqrt(8) * complex(1, 0), 1 / math.sqrt(8) * complex(0, 1), 0, 0, 0, 0, 1 / math.sqrt(4), 1 / math.sqrt(4) * complex(0, 1)] qr = QuantumRegister(4, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_initialize_middle_circuit(self): desired_vector = [0.5, 0.5, 0.5, 0.5] qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") qc = QuantumCircuit(qr, cr) qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.reset(qr[0]) qc.reset(qr[1]) qc.initialize(desired_vector, [qr[0], qr[1]]) qc.measure(qr, cr) # statevector simulator does not support reset shots = 2000 threshold = 0.04 * shots result = wrapper.execute(qc, backend_name='local_qasm_simulator', compile_config={'shots': shots}) counts = result.get_counts() target = { '00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4 } self.assertDictAlmostEqual(counts, target, threshold)
def test_statevector(self): """statevector from a bell state""" q = qiskit.QuantumRegister(2) circ = qiskit.QuantumCircuit(q) circ.h(q[0]) circ.cx(q[0], q[1]) sim_cpp = 'local_statevector_simulator_cpp' sim_py = 'local_statevector_simulator_py' result_cpp = execute(circ, sim_cpp).result() result_py = execute(circ, sim_py).result() statevector_cpp = result_cpp.get_statevector() statevector_py = result_py.get_statevector() fidelity = state_fidelity(statevector_cpp, statevector_py) self.assertGreater( fidelity, self._desired_fidelity, "cpp vs. py statevector has low fidelity{0:.2g}.".format(fidelity))
def test_statevector(self): """statevector from a bell state""" qr = qiskit.QuantumRegister(2) circuit = qiskit.QuantumCircuit(qr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) sim_cpp = 'local_statevector_simulator_cpp' sim_py = 'local_statevector_simulator_py' result_cpp = execute(circuit, sim_cpp).result() result_py = execute(circuit, sim_py).result() statevector_cpp = result_cpp.get_statevector() statevector_py = result_py.get_statevector() fidelity = state_fidelity(statevector_cpp, statevector_py) self.assertGreater( fidelity, self._desired_fidelity, "cpp vs. py statevector has low fidelity{0:.2g}.".format(fidelity))
def test_qasm(self): """counts from a GHZ state""" qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.cx(qr[1], qr[2]) circuit.measure(qr, cr) sim_cpp = Aer.get_backend('qasm_simulator') sim_py = Aer.get_backend('qasm_simulator_py') shots = 2000 result_cpp = execute(circuit, sim_cpp, shots=shots).result() result_py = execute(circuit, sim_py, shots=shots).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots*0.05)
def test_results_save_load(self): """Test saving and loading the results of a circuit. Test for the 'local_unitary_simulator' and 'local_qasm_simulator' """ metadata = {'testval': 5} qr = qiskit.QuantumRegister(2) cr = qiskit.QuantumRegister(2) qc1 = qiskit.QuantumCircuit(qr, cr, name='qc1') qc2 = qiskit.QuantumCircuit(qr, cr, name='qc2') qc1.h(qr) qc2.cx(qr[0], qr[1]) circuits = [qc1, qc2] result1 = execute(circuits, backend='local_unitary_simulator').result() result2 = execute(circuits, backend='local_qasm_simulator').result() test_1_path = self._get_resource_path('test_save_load1.json') test_2_path = self._get_resource_path('test_save_load2.json') # delete these files if they exist if os.path.exists(test_1_path): os.remove(test_1_path) if os.path.exists(test_2_path): os.remove(test_2_path) file1 = file_io.save_result_to_file(result1, test_1_path, metadata=metadata) file2 = file_io.save_result_to_file(result2, test_2_path, metadata=metadata) _, metadata_loaded1 = file_io.load_result_from_file(file1) _, metadata_loaded2 = file_io.load_result_from_file(file1) self.assertAlmostEqual(metadata_loaded1['testval'], 5) self.assertAlmostEqual(metadata_loaded2['testval'], 5) # remove files to keep directory clean os.remove(file1) os.remove(file2)
def test_example_swap_bits(self): """Test a toy example swapping a set bit around. Uses the mapper. Pass if results are correct. """ coupling_map = [[0, 1], [0, 8], [1, 2], [1, 9], [2, 3], [2, 10], [3, 4], [3, 11], [4, 5], [4, 12], [5, 6], [5, 13], [6, 7], [6, 14], [7, 15], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15]] n = 3 # make this at least 3 qr0 = QuantumRegister(n) qr1 = QuantumRegister(n) ans = ClassicalRegister(2 * n) qc = QuantumCircuit(qr0, qr1, ans) # Set the first bit of qr0 qc.x(qr0[0]) # Swap the set bit qc.swap(qr0[0], qr0[n - 1]) qc.swap(qr0[n - 1], qr1[n - 1]) qc.swap(qr1[n - 1], qr0[1]) qc.swap(qr0[1], qr1[1]) # Insert a barrier before measurement qc.barrier() # Measure all of the qubits in the standard basis for j in range(n): qc.measure(qr0[j], ans[j]) qc.measure(qr1[j], ans[j + n]) # First version: no mapping result = execute(qc, backend='local_qasm_simulator', coupling_map=None, shots=1024, seed=14).result() self.assertEqual(result.get_counts(qc), {'010000': 1024}) # Second version: map to coupling graph result = execute(qc, backend='local_qasm_simulator', coupling_map=coupling_map, shots=1024, seed=14).result() self.assertEqual(result.get_counts(qc), {'010000': 1024})
def test_remote_result_fields(self, qe_token, qe_url): """Test components of a result from a remote simulator.""" register(qe_token, qe_url) remote_backend = available_backends({ 'local': False, 'simulator': True })[0] remote_result = execute(self._qc1, remote_backend).result() self.assertEqual(remote_result.backend_name, remote_backend) self.assertIsInstance(remote_result.job_id, str) self.assertEqual(remote_result.status, 'COMPLETED') self.assertEqual(remote_result.circuit_statuses(), ['DONE'])
def test_uniform_superposition(self): desired_vector = [0.5, 0.5, 0.5, 0.5] qr = QuantumRegister(2, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1]]) result = wrapper.execute(qc, backend_name='local_statevector_simulator') statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_ghz_state(self): desired_vector = [1 / math.sqrt(2), 0, 0, 0, 0, 0, 0, 1 / math.sqrt(2)] qr = QuantumRegister(3, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1], qr[2]]) result = wrapper.execute(qc, backend_name='local_statevector_simulator') statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_deterministic_state(self): desired_vector = [0, 1, 0, 0] qr = QuantumRegister(2, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_single_qubit(self): desired_vector = [1/math.sqrt(3), math.sqrt(2)/math.sqrt(3)] qr = QuantumRegister(1, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_single_qubit(self): desired_vector = [1 / math.sqrt(3), math.sqrt(2) / math.sqrt(3)] qr = QuantumRegister(1, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_deterministic_state(self): desired_vector = [0, 1, 0, 0] qr = QuantumRegister(2, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_basic_reordering(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """a simple reordering within a 2-qubit register""" sim, real = self._get_backends(QE_TOKEN, QE_URL, hub, group, project) if not sim or not real: raise unittest.SkipTest('no remote device available') q = qiskit.QuantumRegister(2) c = qiskit.ClassicalRegister(2) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.measure(q[0], c[1]) circ.measure(q[1], c[0]) shots = 2000 result_real = execute(circ, real, {"shots": shots}).result(timeout=600) result_sim = execute(circ, sim, {"shots": shots}).result() counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() self.log.info(counts_real) self.log.info(counts_sim) threshold = 0.1 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_qasm_reset_measure(self): """counts from a qasm program with measure and reset in the middle""" qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.reset(qr[0]) circuit.cx(qr[1], qr[2]) circuit.t(qr) circuit.measure(qr[1], cr[1]) circuit.h(qr[2]) circuit.measure(qr[2], cr[2]) sim_cpp = Aer.get_backend('qasm_simulator') sim_py = Aer.get_backend('qasm_simulator_py') shots = 1000 result_cpp = execute(circuit, sim_cpp, shots=shots, seed=1).result() result_py = execute(circuit, sim_py, shots=shots, seed=1).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.04)
def main(): parser = argparse.ArgumentParser( description="Create randomized circuits for quantum volume analysis.") parser.add_argument('--name', default='quantum_volume', help='circuit name') parser.add_argument('-n', '--qubits', default=5, type=int, help='number of circuit qubits') parser.add_argument('-d', '--depth', default=5, type=int, help='SU(4) circuit depth') parser.add_argument('--num-circ', default=1, type=int, help='how many circuits?') parser.add_argument('-r', '--run', action='store_true', help='execute circuit(s)') parser.add_argument('-b', '--backend', default='local_qasm_simulator', help='backend to execute on') args = parser.parse_args() circuits = build_model_circuits(n=args.qubits, depth=args.depth, num_circ=args.num_circ) # Run the circuits if args.run: backend_name = args.backend if backend_name.startswith("ibmq"): import Qconfig register(Qconfig.APItoken, Qconfig.config['url']) result = execute(circuits, backend_name=backend_name) print(result.get_counts(circuits[0])) return # Save QASM representation of circuits for i in range(num_circ): f = open('quantum_volume_n%d_d%d_i.qasm' % (n, depth, i), 'w') f.write(circuits[i].qasm()) f.close()
def test_qasm(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """counts from a GHZ state""" register(QE_TOKEN, QE_URL, hub, group, project) q = qiskit.QuantumRegister(3) c = qiskit.ClassicalRegister(3) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.cx(q[1], q[2]) circ.measure(q, c) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' sim_ibmq = 'ibmq_qasm_simulator' shots = 2000 result_cpp = execute(circ, sim_cpp, shots=shots).result() result_py = execute(circ, sim_py, shots=shots).result() result_ibmq = execute(circ, sim_ibmq, shots=shots).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots*0.05) self.assertDictAlmostEqual(counts_py, counts_ibmq, shots*0.05)
def test_qasm(self, qe_token, qe_url): """counts from a GHZ state""" qiskit.IBMQ.use_account(qe_token, qe_url) qr = qiskit.QuantumRegister(3) cr = qiskit.ClassicalRegister(3) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.cx(qr[0], qr[1]) circuit.cx(qr[1], qr[2]) circuit.measure(qr, cr) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' sim_ibmq = 'ibmq_qasm_simulator' shots = 2000 result_cpp = execute(circuit, sim_cpp, shots=shots).result() result_py = execute(circuit, sim_py, shots=shots).result() result_ibmq = execute(circuit, sim_ibmq, shots=shots).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots*0.05) self.assertDictAlmostEqual(counts_py, counts_ibmq, shots*0.05)
def test_qasm(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """counts from a GHZ state""" register(QE_TOKEN, QE_URL, hub, group, project) q = qiskit.QuantumRegister(3) c = qiskit.ClassicalRegister(3) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.cx(q[1], q[2]) circ.measure(q, c) sim_cpp = 'local_qasm_simulator_cpp' sim_py = 'local_qasm_simulator_py' sim_ibmq = 'ibmq_qasm_simulator' shots = 2000 result_cpp = execute(circ, sim_cpp, shots=shots).result() result_py = execute(circ, sim_py, shots=shots).result() result_ibmq = execute(circ, sim_ibmq, shots=shots).result() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() counts_ibmq = result_ibmq.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.05) self.assertDictAlmostEqual(counts_py, counts_ibmq, shots * 0.05)
def test_execute(self): """Test Execute. If all correct some should exists. """ backend = get_backend('local_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) job = execute(qc, backend) results = job.result() self.assertIsInstance(results, Result)
def test_random_4qubit(self): desired_vector = [ 1 / math.sqrt(4) * complex(0, 1), 1 / math.sqrt(8) * complex(1, 0), 0, 0, 0, 0, 0, 0, 1 / math.sqrt(8) * complex(1, 0), 1 / math.sqrt(8) * complex(0, 1), 0, 0, 0, 0, 1 / math.sqrt(4) * complex(1, 0), 1 / math.sqrt(8) * complex(1, 0) ] qr = QuantumRegister(4, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_execute_remote(self, QE_TOKEN, QE_URL): """Test Execute remote. If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) backend = provider.available_backends({'simulator': True})[0] qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) job = execute(qc, backend) results = job.result() self.assertIsInstance(results, Result)
def test_combiner(self): desired_vector = [1, 0] qr = QuantumRegister(1, "qr") cr = ClassicalRegister(1, "cr") qc1 = QuantumCircuit(qr, cr) qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]]) qc2 = QuantumCircuit(qr, cr) qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]]) job = wrapper.execute(qc1+qc2, 'local_statevector_simulator') result = job.result() quantum_state = result.get_statevector() fidelity = state_fidelity(quantum_state, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_combiner(self): desired_vector = [0, 1] qr = QuantumRegister(1, "qr") cr = ClassicalRegister(1, "cr") qc1 = QuantumCircuit(qr, cr) qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]]) qc2 = QuantumCircuit(qr, cr) qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]]) job = wrapper.execute(qc1 + qc2, 'local_statevector_simulator') result = job.result() quantum_state = result.get_statevector() fidelity = state_fidelity(quantum_state, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))
def test_execute_remote(self, qe_token, qe_url): """Test Execute remote. If all correct some should exists. """ register(qe_token, qe_url) backend = available_backends({'local': False, 'simulator': True})[0] backend = get_backend(backend) qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) job = execute(qc, backend, seed=TestCompiler.seed) results = job.result() self.assertIsInstance(results, Result)
def test_execute_remote(self, qe_token, qe_url): """Test Execute remote. If all correct some should exists. """ qiskit.IBMQ.use_account(qe_token, qe_url) backend = qiskit.IBMQ.get_backend(local=False, simulator=True) qubit_reg = QuantumRegister(2) clbit_reg = ClassicalRegister(2) qc = QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) job = execute(qc, backend, seed=TestCompiler.seed) results = job.result() self.assertIsInstance(results, Result)
def test_execute_two(self): """Test execute two. If all correct some should exists. """ backend = qiskit.Aer.get_backend('qasm_simulator') qubit_reg = QuantumRegister(2) clbit_reg = ClassicalRegister(2) qc = QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) qc_extra = QuantumCircuit(qubit_reg, clbit_reg) qc_extra.measure(qubit_reg, clbit_reg) job = execute([qc, qc_extra], backend) results = job.result() self.assertIsInstance(results, Result)
def test_initialize_middle_circuit(self): desired_vector = [0.5, 0.5, 0.5, 0.5] qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") qc = QuantumCircuit(qr, cr) qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.reset(qr[0]) qc.reset(qr[1]) qc.initialize(desired_vector, [qr[0], qr[1]]) qc.measure(qr, cr) # statevector simulator does not support reset shots = 2000 threshold = 0.04 * shots job = wrapper.execute(qc, 'local_qasm_simulator', shots=shots) result = job.result() counts = result.get_counts() target = {'00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4} self.assertDictAlmostEqual(counts, target, threshold)
def test_random_3qubit(self): desired_vector = [ 1 / math.sqrt(16) * complex(0, 1), 1 / math.sqrt(8) * complex(1, 0), 1 / math.sqrt(16) * complex(1, 1), 0, 0, 1 / math.sqrt(8) * complex(1, 2), 1 / math.sqrt(16) * complex(1, 0), 0] qr = QuantumRegister(3, "qr") qc = QuantumCircuit(qr) qc.initialize(desired_vector, [qr[0], qr[1], qr[2]]) job = wrapper.execute(qc, 'local_statevector_simulator') result = job.result() statevector = result.get_statevector() fidelity = state_fidelity(statevector, desired_vector) self.assertGreater( fidelity, self._desired_fidelity, "Initializer has low fidelity {0:.2g}.".format(fidelity))