def test_assert_not_uniform(self): """Test AssertUniform with negate True """ qc = QuantumCircuit(2, 2) qc.h(1) bkpt = qc.get_breakpoint_not_uniform([0, 1], [0, 1], 0.00001) BasicAer.backends() job = execute(bkpt, BasicAer.get_backend('qasm_simulator')) self.assertTrue(job.result().get_assertion_passed(bkpt))
def test_assert_product(self): """Test AssertProduct """ qc = QuantumCircuit(2, 2) qc.h(0) bkpt = qc.get_breakpoint_product(0, 0, 1, 1, 0.001) BasicAer.backends() job = execute(bkpt, BasicAer.get_backend('qasm_simulator')) self.assertTrue(job.result().get_assertion_passed(bkpt))
def test_assert_classical(self): """Test AssertClassical """ qc = QuantumCircuit(2, 2) qc.x(1) bkpt = qc.get_breakpoint_classical([0, 1], [0, 1], 0.001, 2) BasicAer.backends() job = execute(bkpt, BasicAer.get_backend('qasm_simulator')) self.assertTrue(job.result().get_assertion_passed(bkpt))
def test_with_registers(self): """Test AssertUniform with register syntax """ q = QuantumRegister(1) q_2 = QuantumRegister(2) c = ClassicalRegister(1) c_2 = ClassicalRegister(2) qc = QuantumCircuit(q, q_2, c, c_2) qc.h([0, 1, 2]) bkpt = qc.get_breakpoint_uniform(q_2, c_2, 0.00001) BasicAer.backends() job = execute(bkpt, BasicAer.get_backend('qasm_simulator')) self.assertTrue(job.result().get_assertion_passed(bkpt))
def test_with_bits(self): """Test AssertProduct with bit syntax """ q = QuantumRegister(1) q_2 = QuantumRegister(2) c = ClassicalRegister(1) c_2 = ClassicalRegister(2) qc = QuantumCircuit(q, q_2, c, c_2) qc.h(0) bkpt = qc.get_breakpoint_product(q[0], c[0], [q_2[0], q_2[1]], [c_2[0], c_2[1]], 0.001) BasicAer.backends() job = execute(bkpt, BasicAer.get_backend('qasm_simulator')) self.assertTrue(job.result().get_assertion_passed(bkpt))
def test_job_qobj(self): """Test job.qobj().""" for backend in BasicAer.backends(): with self.subTest(backend=backend): qobj = assemble_circuits(self.qc1, TranspileConfig(backend=backend)) job = backend.run(qobj) self.assertEqual(job.qobj(), qobj)
def test_deprecated(self): """Test that deprecated names map the same backends as the new names. """ def _get_first_available_backend(provider, backend_names): """Gets the first available backend.""" if isinstance(backend_names, str): backend_names = [backend_names] for backend_name in backend_names: try: return provider.get_backend(backend_name).name() except QiskitBackendNotFoundError: pass return None deprecated_names = BasicAer._deprecated_backend_names() for oldname, newname in deprecated_names.items(): with self.subTest(oldname=oldname, newname=newname): try: resolved_newname = _get_first_available_backend( BasicAer, newname) real_backend = BasicAer.get_backend(resolved_newname) except QiskitBackendNotFoundError: # The real name of the backend might not exist pass else: self.assertEqual( BasicAer.backends(oldname)[0], real_backend)
def test_job_qobj(self): """Test job.qobj().""" for backend in BasicAer.backends(): with self.subTest(backend=backend): qobj = assemble(self.qc1) job = backend.run(qobj) self.assertEqual(job.qobj(), qobj)
def test_deprecated(self): """Test that deprecated names map the same backends as the new names. """ def _get_first_available_backend(provider, backend_names): """Gets the first available backend.""" if isinstance(backend_names, str): backend_names = [backend_names] for backend_name in backend_names: try: return provider.get_backend(backend_name).name() except QiskitBackendNotFoundError: pass return None deprecated_names = BasicAer._deprecated_backend_names() for oldname, newname in deprecated_names.items(): expected = "WARNING:qiskit.providers.providerutils:Backend '%s' is deprecated. " \ "Use '%s'." % (oldname, newname) with self.subTest(oldname=oldname, newname=newname): with self.assertLogs('qiskit.providers.providerutils', level='WARNING') as context: resolved_newname = _get_first_available_backend( BasicAer, newname) real_backend = BasicAer.get_backend(resolved_newname) self.assertEqual( BasicAer.backends(oldname)[0], real_backend) self.assertEqual(context.output, [expected])
def test_get_backend(self): """Test get backends. If all correct should return a name the same as input. """ backend = BasicAer.backends(name='qasm_simulator')[0] self.assertEqual(backend.name(), 'qasm_simulator')
def test_builtin_simulators_backend_properties(self): """Test backend properties. If all correct should pass the validation. """ simulators = BasicAer.backends() for backend in simulators: properties = backend.properties() self.assertEqual(properties, None)
def test_builtin_simulators_backend_status(self): """Test backend_status.""" 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 BasicAer.backends(): status = backend.status() jsonschema.validate(status.to_dict(), schema)
def get_aer_backends(): try: backends = LegacySimulators.backends() logger.debug('Using LegacySimulators backends.') return backends except: pass backends = BasicAer.backends() logger.debug('Using BasicAer backends.') return backends
def test_builtin_simulators_backend_configuration(self): """Test backend configuration.""" 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 = BasicAer.backends() for backend in builtin_simulators: configuration = backend.configuration() jsonschema.validate(configuration.to_dict(), schema)
def test_qobj_headers_in_result(self): """Test that the qobj headers are passed onto the results.""" custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend in BasicAer.backends(): with self.subTest(backend=backend): new_circ = transpile(self.qc1, backend=backend) qobj = assemble(new_circ, shots=1024) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' result = backend.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def test_deprecated(self): """Test that deprecated names map the same backends as the new names. """ deprecated_names = BasicAer._deprecated_backend_names() for oldname, newname in deprecated_names.items(): with self.subTest(oldname=oldname, newname=newname): try: resolved_newname = _get_first_available_backend( BasicAer, newname) real_backend = BasicAer.get_backend(resolved_newname) except QiskitBackendNotFoundError: # The real name of the backend might not exist pass else: self.assertEqual( BasicAer.backends(oldname)[0], real_backend)
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(BasicAer.backends()) # running the job job_sim = execute([qc1, qc2], BasicAer.get_backend('qasm_simulator')) sim_result = job_sim.result() # Show the results 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 try:
IBMQ.load_accounts() # Making first circuit: bell state qc1 = QuantumCircuit(2, 2, name="bell") qc1.h(0) qc1.cx(0, 1) qc1.measure([0, 1], [0, 1]) # Making another circuit: superpositions qc2 = QuantumCircuit(2, 2, name="superposition") qc2.h([0, 1]) qc2.measure([0, 1], [0, 1]) # Setting up the backend print("(Aer Backends)") for backend in BasicAer.backends(): print(backend.status()) qasm_simulator = BasicAer.get_backend('qasm_simulator') # Compile and run the circuit on a real device backend # See a list of available remote backends print("\n(IBMQ Backends)") for backend in IBMQ.backends(): print(backend.status()) try: # select least busy available device and execute. least_busy_device = least_busy(IBMQ.backends(simulator=False)) except: print("All devices are currently unavailable.")
def test_aliases_return_empty_list(self): """Test backends() return an empty list if name is unknown.""" self.assertEqual(BasicAer.backends("bad_name"), [])
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("Simuladores Disponibles: ", BasicAer.backends()) backend_sim = BasicAer.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('Resultado: {}'.format(str(result_sim.get_counts(qc)))) print('Estado: {}'.format(job_sim.status())) # see a list of available remote backends ibmq_backends = IBMQ.backends() print("Simuladores Remotos: ", ibmq_backends) # Compile and run the Quantum Program on a real device backend
import argparse import os import sys import datetime from qiskit import IBMQ, BasicAer IBMQ_SIMULATOR = 'ibmq_qasm_simulator' LOCAL_SIMULATOR = 'qasm_simulator' SIMULATORS = [IBMQ_SIMULATOR, LOCAL_SIMULATOR] MAX_JOBS_PER_ONE = 70 BACKENDS = BasicAer.backends( ) # In set_parameters() BACKENDS will be supplemented IBMQ.backends() def chunks(l, n): """Yield successive n-sized chunks from l.""" for i in range(0, len(l), n): yield l[i:i + n] class DefaultArgumentParser(argparse.ArgumentParser): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.add_argument('-t', '--token', default=None, help='Specific token') self.add_argument('-b', '--backend', type=str, default=LOCAL_SIMULATOR, help='Name of backend (default: %(default)s)') self.add_argument(
def test_builtin_simulators_backend_properties(self): """Test backend properties.""" simulators = BasicAer.backends() for backend in simulators: properties = backend.properties() self.assertEqual(properties, None)
Udisg(udis, lam, q0, q1, q2, q3) mes.measure(q0, c0) mes.measure(q1, c1) mes.measure(q2, c2) mes.measure(q3, c3) return ini + udis + mes try: IBMQ.load_accounts() except: print("""WARNING: There's no connection with the API for remote backends. Have you initialized a file with your personal token? For now, there's only access to local simulator backends...""") print("IBMQ backends: ", IBMQ.backends()) print("IBMQ backends: ", BasicAer.backends()) backend_sim = BasicAer.get_backend('qasm_simulator') print("Connected to" + str(backend_sim)) shots = 1024 coupling_map = None mag_sim = [] for i in range(8): q = QuantumRegister(4, "q") c = ClassicalRegister(4, "c") Udis = QuantumCircuit(q, c) ini = QuantumCircuit(q, c) mes = QuantumCircuit(q, c) lam = i * 0.25 sumter = Ising(ini, Udis, mes, lam, q[0], q[1], q[2], q[3], c[0], c[1], c[2], c[3])
def test_get_backend(self): """Test get backends.""" backend = BasicAer.backends(name='qasm_simulator')[0] self.assertEqual(backend.name(), 'qasm_simulator')
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("BasicAer backends: ", BasicAer.backends()) backend_sim = BasicAer.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
# To check we applied the correct operations we can draw it! # In[4]: qc.draw() # ### Backend # # Once we have the circuit we need to choose our backend. This will determine how we run the circuit. You can either do this numerically (so you output the state vector or unitary exactly), simulate a quantum computer (so you output fake, noisey, data) or run it on a real quanutm computer. For our purpose we will moslty be generating fake data, but lets check out the other two: # In[5]: BasicAer.backends() # #### Numerical Simulation (statevector) # In[6]: # load the backend backend = BasicAer.get_backend('statevector_simulator') # create a program to run job = execute(qc, backend) # get the results result = job.result()