def test_circuit_multi(self): """Test circuit multi regs declared at start. """ qreg0 = QuantumRegister(2, 'q0') creg0 = ClassicalRegister(2, 'c0') qreg1 = QuantumRegister(2, 'q1') creg1 = ClassicalRegister(2, 'c1') circ = QuantumCircuit(qreg0, qreg1) circ.x(qreg0[1]) circ.x(qreg1[0]) meas = QuantumCircuit(qreg0, qreg1, creg0, creg1) meas.measure(qreg0, creg0) meas.measure(qreg1, creg1) qc = circ + meas backend_sim = Simulators.get_backend('qasm_simulator') qobj_qc = compile(qc, backend_sim, seed_mapper=34342) qobj_circ = compile(circ, backend_sim, seed_mapper=3438) result = backend_sim.run(qobj_qc).result() counts = result.get_counts(qc) backend_sim = LegacySimulators.get_backend('qasm_simulator') result = backend_sim.run(qobj_qc).result() counts_py = result.get_counts(qc) target = {'01 10': 1024} backend_sim = LegacySimulators.get_backend('statevector_simulator') result = backend_sim.run(qobj_circ).result() state = result.get_statevector(circ) backend_sim = Simulators.get_backend('statevector_simulator') result = backend_sim.run(qobj_circ).result() state_py = result.get_statevector(circ) backend_sim = Simulators.get_backend('unitary_simulator') result = backend_sim.run(qobj_circ).result() unitary = result.get_unitary(circ) self.assertEqual(counts, target) self.assertEqual(counts_py, target) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state), 1.0, places=7) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state_py), 1.0, places=7) self.assertAlmostEqual(process_fidelity( Pauli(label='IXXI').to_matrix(), unitary), 1.0, places=7)
def test_get_backend(self): """Test get backends. If all correct should return a name the same as input. """ backend = Simulators.backends(name='qasm_simulator')[0] self.assertEqual(backend.name(), 'qasm_simulator')
def _tomography_test_data(circuit, qr, cr, tomoset, shots): tomo_circs = tomo.create_tomography_circuits(circuit, qr, cr, tomoset) result = execute(tomo_circs, Simulators.get_backend('qasm_simulator'), shots=shots, seed=42).result() data = tomo.tomography_data(result, circuit.name, tomoset) return tomo.fit_tomography_data(data)
def test_qobj_to_circuits_single_no_qasm(self): """Check that qobj_to_circuits's result matches the qobj ini.""" backend = Simulators.get_backend('qasm_simulator') qobj_in = compile(self.circuit, backend, pass_manager=PassManager()) for i in qobj_in.experiments: del i.header.compiled_circuit_qasm out_circuit = qobj_to_circuits(qobj_in) self.assertEqual(circuit_to_dag(out_circuit[0]), self.dag)
def test_random_state_circuit(self): state = random_state(3) q = QuantumRegister(3) qc = QuantumCircuit(q) qc.initialize(state, [q[0], q[1], q[2]]) backend = Simulators.get_backend('statevector_simulator') qc_state = execute(qc, backend).result().get_statevector(qc) self.assertAlmostEqual(state_fidelity(qc_state, state), 1.0, places=7)
def test_builtin_simulators_backend_properties(self): """Test backend properties. If all correct should pass the validation. """ simulators = Simulators.backends() for backend in simulators: properties = backend.properties() self.assertEqual(properties, None)
def test_circuit_multi_case2(self): """Test circuit multi regs declared at start. """ qreg0 = QuantumRegister(2, 'q0') creg0 = ClassicalRegister(2, 'c0') qreg1 = QuantumRegister(2, 'q1') creg1 = ClassicalRegister(2, 'c1') circ2 = QuantumCircuit() circ2.add_register(qreg0) circ2.add_register(qreg1) circ2.x(qreg0[1]) circ2.x(qreg1[0]) meas2 = QuantumCircuit() meas2.add_register(qreg0) meas2.add_register(qreg1) meas2.add_register(creg0) meas2.add_register(creg1) meas2.measure(qreg0, creg0) meas2.measure(qreg1, creg1) qc2 = circ2 + meas2 backend_sim = Simulators.get_backend('statevector_simulator') result = execute(circ2, backend_sim).result() state = result.get_statevector(circ2) backend_sim = Simulators.get_backend('qasm_simulator') result = execute(qc2, backend_sim).result() counts = result.get_counts(qc2) backend_sim = Simulators.get_backend('unitary_simulator') result = execute(circ2, backend_sim).result() unitary = result.get_unitary(circ2) target = {'01 10': 1024} self.assertEqual(target, counts) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state), 1.0, places=7) self.assertAlmostEqual(process_fidelity( Pauli(label='IXXI').to_matrix(), unitary), 1.0, places=7)
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 = Simulators.get_backend('qasm_simulator') self._result1 = qiskit.execute(self._qc1, self.backend).result() self._result2 = qiskit.execute(self._qc2, self.backend).result()
def test_builtin_unitary_simulator_py(self): backend = Simulators.get_backend('unitary_simulator') qobj = compile(self.circuits, backend=backend) exp = qobj.experiments[0] c_qasm = exp.header.compiled_circuit_qasm self.assertIn(self.qr_name, map(lambda x: x[0], exp.header.qubit_labels)) self.assertIn(self.qr_name, c_qasm) self.assertIn(self.cr_name, map(lambda x: x[0], exp.header.clbit_labels)) self.assertIn(self.cr_name, c_qasm)
def test_job_monitor(self): """Test job_monitor""" qreg = QuantumRegister(2) creg = ClassicalRegister(2) qc = QuantumCircuit(qreg, creg) qc.h(qreg[0]) qc.cx(qreg[0], qreg[1]) qc.measure(qreg, creg) backend = Simulators.get_backend('qasm_simulator') job_sim = execute([qc] * 10, backend) qiskit.tools.job_monitor(job_sim) self.assertEqual(job_sim.status().name, 'DONE')
def test_builtin_simulators_backend_status(self): """Test backend_status. If all correct should pass the validation. """ schema_path = self._get_resource_path( 'backend_status_schema.json', path=Path.SCHEMAS) with open(schema_path, 'r') as schema_file: schema = json.load(schema_file) for backend in Simulators.backends(): status = backend.status() jsonschema.validate(status.to_dict(), schema)
def test_qobj_to_circuit_with_sim_instructions(self): """Check qobj_to_circuit result with asimulator instruction.""" backend = Simulators.get_backend('qasm_simulator') qr = QuantumRegister(3) cr = ClassicalRegister(3) circuit = QuantumCircuit(qr, cr) circuit.ccx(qr[0], qr[1], qr[2]) circuit.snapshot(1) circuit.measure(qr, cr) dag = circuit_to_dag(circuit) qobj_in = compile(circuit, backend, pass_manager=PassManager()) out_circuit = qobj_to_circuits(qobj_in) self.assertEqual(circuit_to_dag(out_circuit[0]), dag)
def test_builtin_simulators_backend_configuration(self): """Test backend configuration. If all correct should pass the validation. """ schema_path = self._get_resource_path( 'backend_configuration_schema.json', path=Path.SCHEMAS) with open(schema_path, 'r') as schema_file: schema = json.load(schema_file) builtin_simulators = Simulators.backends() for backend in builtin_simulators: configuration = backend.configuration() jsonschema.validate(configuration.to_dict(), schema)
def setUp(self): super().setUp() self._testing_device = os.getenv('IBMQ_QOBJ_DEVICE', None) self._qe_token = os.getenv('IBMQ_TOKEN', None) self._qe_url = os.getenv('IBMQ_QOBJ_URL') if not self._testing_device or not self._qe_token or not self._qe_url: self.skipTest("No credentials or testing device available for " "testing Qobj capabilities.") IBMQ.enable_account(self._qe_token, self._qe_url) self._local_backend = Simulators.get_backend('qasm_simulator') self._remote_backend = IBMQ.get_backend(self._testing_device) self.log.info('Remote backend: %s', self._remote_backend.name()) self.log.info('Local backend: %s', self._local_backend.name())
def test_extend_circuit_different_registers(self): """Test extending a circuit with different registers. """ qr = QuantumRegister(2) cr = ClassicalRegister(2) qc1 = QuantumCircuit(qr) qc1.x(qr) qc2 = QuantumCircuit(qr, cr) qc2.measure(qr, cr) qc1 += qc2 backend = Simulators.get_backend('qasm_simulator') shots = 1024 result = execute(qc1, backend=backend, shots=shots, seed=78).result() counts = result.get_counts() target = {'11': shots} self.assertEqual(counts, target)
def test_qobj_to_circuit_with_parameters(self): """Check qobj_to_circuit result with a gate that uses parameters.""" backend = Simulators.get_backend('qasm_simulator') qreg1 = QuantumRegister(2) qreg2 = QuantumRegister(3) creg1 = ClassicalRegister(2) creg2 = ClassicalRegister(2) circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2) circuit_b.x(qreg1) circuit_b.h(qreg2) circuit_b.u2(0.2, 0.57, qreg2[1]) circuit_b.measure(qreg1, creg1) circuit_b.measure(qreg2[0], creg2[1]) qobj = compile(circuit_b, backend, pass_manager=PassManager()) out_circuit = qobj_to_circuits(qobj) self.assertEqual(circuit_to_dag(out_circuit[0]), circuit_to_dag(circuit_b))
def test_qobj_to_circuits_multiple(self): """Check that qobj_to_circuits's result with multiple circuits""" backend = Simulators.get_backend('qasm_simulator') qreg1 = QuantumRegister(2) qreg2 = QuantumRegister(3) creg1 = ClassicalRegister(2) creg2 = ClassicalRegister(2) circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2) circuit_b.x(qreg1) circuit_b.h(qreg2) circuit_b.measure(qreg1, creg1) circuit_b.measure(qreg2[0], creg2[1]) qobj = compile([self.circuit, circuit_b], backend, pass_manager=PassManager()) dag_list = [circuit_to_dag(x) for x in qobj_to_circuits(qobj)] self.assertEqual(dag_list, [self.dag, circuit_to_dag(circuit_b)])
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 = LegacySimulators.get_backend('statevector_simulator') sim_py = Simulators.get_backend('statevector_simulator') 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 = LegacySimulators.get_backend('qasm_simulator') sim_py = Simulators.get_backend('qasm_simulator') 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.08)
def test_extend_circuit(self): """Test extending a circuit with same registers. """ qr = QuantumRegister(2) cr = ClassicalRegister(2) qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr[0]) qc1.measure(qr[0], cr[0]) qc2.measure(qr[1], cr[1]) qc1 += qc2 backend = Simulators.get_backend('qasm_simulator') shots = 1024 result = execute(qc1, backend=backend, shots=shots, seed=78).result() counts = result.get_counts() target = {'00': shots / 2, '01': shots / 2} threshold = 0.04 * shots self.assertDictAlmostEqual(counts, target, threshold)
def test_change_qobj_after_compile(self): """Test modifying Qobj parameters after compile.""" qr = QuantumRegister(3) cr = ClassicalRegister(3) qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr[0]) qc1.cx(qr[0], qr[1]) qc1.cx(qr[0], qr[2]) qc2.h(qr) qc1.measure(qr, cr) qc2.measure(qr, cr) circuits = [qc1, qc2] shots = 1024 backend = Simulators.get_backend('qasm_simulator') config = {'seed': 10, 'shots': 1, 'xvals': [1, 2, 3, 4]} qobj1 = compile(circuits, backend=backend, shots=shots, seed=88, config=config) qobj1.experiments[0].config.shots = 50 qobj1.experiments[0].config.xvals = [1, 1, 1] config['shots'] = 1000 config['xvals'][0] = 'only for qobj2' qobj2 = compile(circuits, backend=backend, shots=shots, seed=88, config=config) self.assertTrue(qobj1.experiments[0].config.shots == 50) self.assertTrue(qobj1.experiments[1].config.shots == 1) self.assertTrue(qobj1.experiments[0].config.xvals == [1, 1, 1]) self.assertTrue(qobj1.experiments[1].config.xvals == [1, 2, 3, 4]) self.assertTrue(qobj1.config.shots == 1024) self.assertTrue(qobj2.experiments[0].config.shots == 1000) self.assertTrue(qobj2.experiments[1].config.shots == 1000) self.assertTrue( qobj2.experiments[0].config.xvals == ['only for qobj2', 2, 3, 4]) self.assertTrue( qobj2.experiments[1].config.xvals == ['only for qobj2', 2, 3, 4])
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 = LegacySimulators.get_backend('qasm_simulator') sim_py = Simulators.get_backend('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() counts_cpp = result_cpp.get_counts() counts_py = result_py.get_counts() self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.06)
def test_average_data(self): """Test average_data.""" qr = qiskit.QuantumRegister(2) cr = qiskit.ClassicalRegister(2) qc = qiskit.QuantumCircuit(qr, cr, name="qc") qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.measure(qr[0], cr[0]) qc.measure(qr[1], cr[1]) shots = 10000 backend = Simulators.get_backend('qasm_simulator') result = qiskit.execute(qc, backend, shots=shots).result() counts = result.get_counts(qc) observable = {"00": 1, "11": 1, "01": -1, "10": -1} mean_zz = average_data(counts=counts, observable=observable) observable = {"00": 1, "11": -1, "01": 1, "10": -1} mean_zi = average_data(counts, observable) observable = {"00": 1, "11": -1, "01": -1, "10": 1} mean_iz = average_data(counts, observable) self.assertAlmostEqual(mean_zz, 1, places=1) self.assertAlmostEqual(mean_zi, 0, places=1) self.assertAlmostEqual(mean_iz, 0, places=1)
q = QuantumRegister(2) # Create a Classical Register with 2 bits. c = ClassicalRegister(2) # Create a Quantum Circuit qc = QuantumCircuit(q, c) # Add a H gate on qubit 0, putting this qubit in superposition. qc.h(q[0]) # Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting # the qubits in a Bell state. qc.cx(q[0], q[1]) # Add a Measure gate to see the state. qc.measure(q, c) # See a list of available local simulators print("Aer backends: ", Simulators.backends()) backend_sim = Simulators.get_backend('qasm_simulator') # Compile and run the Quantum circuit on a simulator backend job_sim = execute(qc, backend_sim) result_sim = job_sim.result() # Show the results print("simulation: ", result_sim) print(result_sim.get_counts(qc)) # see a list of available remote backends ibmq_backends = IBMQ.backends() print("Remote backends: ", ibmq_backends) # Compile and run the Quantum Program on a real device backend
def setUp(self): self.seed = 42 self.backend = Simulators.get_backend("qasm_simulator")
def test_aliases_return_empty_list(self): """Test backends() return an empty list if name is unknown.""" self.assertEqual(Simulators.backends("bad_name"), [])
def test_qobj_to_circuits_single(self): """Check that qobj_to_circuits's result matches the qobj ini.""" backend = Simulators.get_backend('qasm_simulator') qobj_in = compile(self.circuit, backend, pass_manager=PassManager()) out_circuit = qobj_to_circuits(qobj_in) self.assertEqual(circuit_to_dag(out_circuit[0]), self.dag)
clbit_reg = ClassicalRegister(2) # making first circuit: bell state qc1 = QuantumCircuit(qubit_reg, clbit_reg) qc1.h(qubit_reg[0]) qc1.cx(qubit_reg[0], qubit_reg[1]) qc1.measure(qubit_reg, clbit_reg) # making another circuit: superpositions qc2 = QuantumCircuit(qubit_reg, clbit_reg) qc2.h(qubit_reg) qc2.measure(qubit_reg, clbit_reg) # setting up the backend print("(AER Backends)") print(Simulators.backends()) # running the job job_sim = execute([qc1, qc2], Simulators.get_backend('qasm_simulator')) sim_result = job_sim.result() # Show the results print("simulation: ", sim_result) print(sim_result.get_counts(qc1)) print(sim_result.get_counts(qc2)) # see a list of available remote backends print("\n(IBMQ Backends)") print(IBMQ.backends()) # Compile and run on a real device backend
clbit_reg = ClassicalRegister(2, name='c') # Making first circuit: bell state qc1 = QuantumCircuit(qubit_reg, clbit_reg, name="bell") qc1.h(qubit_reg[0]) qc1.cx(qubit_reg[0], qubit_reg[1]) qc1.measure(qubit_reg, clbit_reg) # Making another circuit: superpositions qc2 = QuantumCircuit(qubit_reg, clbit_reg, name="superposition") qc2.h(qubit_reg) qc2.measure(qubit_reg, clbit_reg) # Setting up the backend print("(Aer Backends)") for backend in Simulators.backends(): print(backend.status()) my_backend = Simulators.get_backend('local_qasm_simulator') print("(QASM Simulator configuration) ") pprint.pprint(my_backend.configuration()) print("(QASM Simulator properties) ") pprint.pprint(my_backend.properties()) print("\n(IMQ Backends)") for backend in IBMQ.backends(): print(backend.status()) # select least busy available device and execute. least_busy_device = least_busy(IBMQ.backends(simulator=False)) print("Running on current least busy device: ", least_busy_device) print("(with configuration) ")
""" Example on how to load a file into a QuantumCircuit """ from qiskit import QuantumCircuit from qiskit import QiskitError, execute, Simulators try: circ = QuantumCircuit.from_qasm_file( "examples/qasm/entangled_registers.qasm") print(circ.draw()) # See the backend sim_backend = Simulators.get_backend('qasm_simulator') # Compile and run the Quantum circuit on a local simulator backend job_sim = execute(circ, sim_backend) sim_result = job_sim.result() # Show the results print("simulation: ", sim_result) print(sim_result.get_counts(circ)) except QiskitError as ex: print('There was an internal Qiskit error. Error = {}'.format(ex))