def test_compact(self):
     """test the compact flag for available_backends works"""
     compact_names = available_backends()
     expanded_names = available_backends(compact=False)
     self.assertIn('local_qasm_simulator', compact_names)
     self.assertIn('local_statevector_simulator', compact_names)
     self.assertIn('local_unitary_simulator', compact_names)
     self.assertIn('local_qasm_simulator_py', expanded_names)
     self.assertIn('local_statevector_simulator_py', expanded_names)
 def test_compact(self):
     """test the compact flag for available_backends works"""
     compact_names = available_backends()
     expanded_names = available_backends(compact=False)
     self.assertIn('local_qasm_simulator', compact_names)
     self.assertIn('local_statevector_simulator', compact_names)
     self.assertIn('local_unitary_simulator', compact_names)
     self.assertIn('local_qasm_simulator_py', expanded_names)
     self.assertIn('local_statevector_simulator_py', expanded_names)
Esempio n. 3
0
    def register_and_get_operational_backends(qconfig):
        if qconfig is not None:
            hub = qconfig.config.get('hub', None)
            group = qconfig.config.get('group', None)
            project = qconfig.config.get('project', None)
            proxies = qconfig.config.get('proxies', None)
            verify = qconfig.config.get('verify', True)
            provider_name = qconfig.config.get('provider_name', 'ibmq')
            try:
                q_register(qconfig.APItoken,
                           qconfig.config["url"],
                           hub=hub,
                           group=group,
                           project=project,
                           proxies=proxies,
                           verify=verify,
                           provider_name=provider_name)
                logger.debug('Registered with QISKit successfully.')
            except Exception as e:
                logger.debug('Failed to register quantum backends: {}'.format(
                    str(e)))

        backends = available_backends()
        backends = [
            x for x in backends
            if x not in QuantumAlgorithm.UNSUPPORTED_BACKENDS
        ]
        return backends
Esempio n. 4
0
    def register_and_get_operational_backends(qconfig):
        try:
            for provider in q_registered_providers():
                if isinstance(provider,IBMQProvider):
                    q_unregister(provider)
                    logger.debug("Provider 'IBMQProvider' unregistered with Qiskit successfully.")
                    break
        except Exception as e:
                logger.debug("Failed to unregister provider 'IBMQProvider' with Qiskit: {}".format(str(e)))

        if qconfig is not None:
            hub = qconfig.config.get('hub', None)
            group = qconfig.config.get('group', None)
            project = qconfig.config.get('project', None)
            proxies = qconfig.config.get('proxies', None)
            verify = qconfig.config.get('verify', True)
            try:
                q_register(qconfig.APItoken,
                           provider_class=IBMQProvider,
                           url=qconfig.config["url"],
                           hub=hub,
                           group=group,
                           project=project,
                           proxies=proxies,
                           verify=verify)
                logger.debug("Provider 'IBMQProvider' registered with Qiskit successfully.")
            except Exception as e:
                logger.debug("Failed to register provider 'IBMQProvider' with Qiskit: {}".format(str(e)))

        backends = available_backends()
        backends = [x for x in backends if x not in QuantumAlgorithm.UNSUPPORTED_BACKENDS]
        return backends
Esempio n. 5
0
    def test_compile_two_run_remote(self,
                                    QE_TOKEN,
                                    QE_URL,
                                    hub=None,
                                    group=None,
                                    project=None):
        """Test Compiler and run two circuits.

        If all correct some should exists.
        """
        register(QE_TOKEN, QE_URL, hub, group, project)
        backend = available_backends({'local': False, 'simulator': True})[0]
        backend = get_backend(backend)
        qubit_reg = qiskit.QuantumRegister(2, name='q')
        clbit_reg = qiskit.ClassicalRegister(2, name='c')
        qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell")
        qc.h(qubit_reg[0])
        qc.cx(qubit_reg[0], qubit_reg[1])
        qc.measure(qubit_reg, clbit_reg)
        qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra")
        qc_extra.measure(qubit_reg, clbit_reg)
        qobj = transpiler.compile([qc, qc_extra], backend)
        job = backend.run(qobj)
        result = job.result()
        self.assertIsInstance(result, Result)
Esempio n. 6
0
    def register_and_get_operational_backends(qconfig):
        if qconfig is not None:
            hub = qconfig.config.get('hub', None)
            group = qconfig.config.get('group', None)
            project = qconfig.config.get('project', None)
            proxies = qconfig.config.get('proxies', None)
            verify = qconfig.config.get('verify', True)
            provider_name = qconfig.config.get('provider_name', 'ibmq')
            try:
                q_register(qconfig.APItoken,
                           qconfig.config["url"],
                           hub=hub,
                           group=group,
                           project=project,
                           proxies=proxies,
                           verify=verify,
                           provider_name=provider_name)
            except:
                logger.debug(
                    "WARNING: Can not register quantum backends. Check your Qconfig.py."
                )

        backends = available_backends()
        backends = [
            x for x in backends
            if x not in QuantumAlgorithm.UNSUPPORTED_BACKENDS
        ]
        return backends
Esempio n. 7
0
    def test_compile_two_remote(self,
                                QE_TOKEN,
                                QE_URL,
                                hub=None,
                                group=None,
                                project=None):
        """Test Compiler remote on two circuits.

        If all correct some should exists.
        """
        register(QE_TOKEN, QE_URL, hub, group, project)
        backend = least_busy(available_backends())
        backend = get_backend(backend)

        qubit_reg = qiskit.QuantumRegister(2, name='q')
        clbit_reg = qiskit.ClassicalRegister(2, name='c')
        qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell")
        qc.h(qubit_reg[0])
        qc.cx(qubit_reg[0], qubit_reg[1])
        qc.measure(qubit_reg, clbit_reg)
        qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra")
        qc_extra.measure(qubit_reg, clbit_reg)
        qobj = transpiler.compile([qc, qc_extra], backend)

        # FIXME should test against the qobj when defined
        self.assertEqual(len(qobj), 3)
    def test_filter_config_dict(self, qe_token, qe_url):
        """Test filtering by dictionary of configuration properties"""
        n_qubits = 20 if self.using_ibmq_credentials else 5

        register(qe_token, qe_url)
        filter_ = {'n_qubits': n_qubits, 'local': False}
        filtered_backends = available_backends(filter_)
        self.assertTrue(filtered_backends)
Esempio n. 9
0
def lowest_pending_jobs():
    """Returns the backend with lowest pending jobs."""
    list_of_backends = available_backends(
        {'local': False, 'simulator': False})
    device_status = [get_backend(backend).status for backend in list_of_backends]

    best = min([x for x in device_status if x['available'] is True],
               key=lambda x: x['pending_jobs'])
    return best['name']
 def test_filter_least_busy(self,
                            QE_TOKEN,
                            QE_URL,
                            hub=None,
                            group=None,
                            project=None):
     """Test filtering by least busy function"""
     register(QE_TOKEN, QE_URL, hub, group, project)
     filtered_backends = least_busy(available_backends())
     self.assertTrue(filtered_backends)
Esempio n. 11
0
    def _get_backends(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None):
        sim_backend = 'local_qasm_simulator'
        try:
            register(QE_TOKEN, QE_URL, hub, group, project)
            real_backends = available_backends({'simulator': False})
            real_backend = lowest_pending_jobs(real_backends)
        except Exception:
            real_backend = None

        return sim_backend, real_backend
Esempio n. 12
0
    def _get_backends(self, qe_token, qe_url):
        sim_backend = 'local_qasm_simulator'
        try:
            register(qe_token, qe_url)
            real_backends = available_backends({'simulator': False})
            real_backend = least_busy(real_backends)
        except Exception:
            real_backend = None

        return sim_backend, real_backend
    def _get_backends(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None):
        sim_backend = 'local_qasm_simulator'
        try:
            register(QE_TOKEN, QE_URL, hub, group, project)
            real_backends = available_backends({'simulator': False})
            real_backend = lowest_pending_jobs(real_backends)
        except Exception:
            real_backend = None

        return sim_backend, real_backend
Esempio n. 14
0
    def _get_backends(self, QE_TOKEN, QE_URL):
        sim_backend = 'local_qasm_simulator'
        try:
            register(QE_TOKEN, QE_URL)
            real_backends = available_backends({'simulator': False})
            real_backend = least_busy(real_backends)
        except Exception:
            real_backend = None

        return sim_backend, real_backend
 def test_filter_status_config_dict(self,
                                    QE_TOKEN,
                                    QE_URL,
                                    hub=None,
                                    group=None,
                                    project=None):
     """Test filtering by dictionary of mixed status/configuration properties"""
     register(QE_TOKEN, QE_URL, hub, group, project)
     filter_ = {'available': True, 'local': False, 'simulator': True}
     filtered_backends = available_backends(filter_)
     self.assertTrue(filtered_backends)
 def test_filter_config_callable(self,
                                 QE_TOKEN,
                                 QE_URL,
                                 hub=None,
                                 group=None,
                                 project=None):
     """Test filtering by lambda function on configuration properties"""
     register(QE_TOKEN, QE_URL, hub, group, project)
     filtered_backends = available_backends(lambda x: (not x.configuration[
         'simulator'] and x.configuration['n_qubits'] > 5))
     self.assertTrue(filtered_backends)
 def test_filter_config_dict(self,
                             QE_TOKEN,
                             QE_URL,
                             hub=None,
                             group=None,
                             project=None):
     """Test filtering by dictionary of configuration properties"""
     register(QE_TOKEN, QE_URL, hub, group, project)
     filter_ = {'n_qubits': 5, 'local': False}
     filtered_backends = available_backends(filter_)
     self.assertTrue(filtered_backends)
Esempio n. 18
0
 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'])
Esempio n. 19
0
    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)
Esempio n. 20
0
    def test_execute_remote(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None):
        """Test Execute remote.

        If all correct some should exists.
        """
        register(QE_TOKEN, QE_URL, hub, group, project)
        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)
        results = job.result()
        self.assertIsInstance(results, Result)
Esempio n. 21
0
    def test_compile_run_remote(self, qe_token, qe_url):
        """Test Compiler and run 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, name='q')
        clbit_reg = qiskit.ClassicalRegister(2, name='c')
        qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell")
        qc.h(qubit_reg[0])
        qc.cx(qubit_reg[0], qubit_reg[1])
        qc.measure(qubit_reg, clbit_reg)
        qobj = transpiler.compile(qc, backend, seed=TestCompiler.seed)
        job = backend.run(qobj)
        result = job.result(timeout=20)
        self.assertIsInstance(result, Result)
Esempio n. 22
0
    def test_compile_remote(self, qe_token, qe_url):
        """Test Compiler remote.

        If all correct some should exists.
        """
        register(qe_token, qe_url)
        backend = least_busy(available_backends())
        backend = get_backend(backend)

        qubit_reg = qiskit.QuantumRegister(2, name='q')
        clbit_reg = qiskit.ClassicalRegister(2, name='c')
        qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell")
        qc.h(qubit_reg[0])
        qc.cx(qubit_reg[0], qubit_reg[1])
        qc.measure(qubit_reg, clbit_reg)

        qobj = transpiler.compile(qc, backend)

        # FIXME should validate the Qobj when defined
        self.assertIsInstance(qobj, Qobj)
Esempio n. 23
0
    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("(Local Backends)")
    for backend_name in available_backends({'local': True}):
        backend = get_backend(backend_name)
        print(backend.status)
    my_backend_name = 'local_qasm_simulator'
    my_backend = get_backend(my_backend_name)
    print("(Local QASM Simulator configuration) ")
    pprint.pprint(my_backend.configuration)
    print("(Local QASM Simulator calibration) ")
    pprint.pprint(my_backend.calibration)
    print("(Local QASM Simulator parameters) ")
    pprint.pprint(my_backend.parameters)

    # Compiling the job
    qobj = compile([qc1, qc2], my_backend)
    # I think we need to make a qobj into a class
Esempio n. 24
0
    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("Local backends: ", available_backends({'local': True}))

    # Compile and run the Quantum circuit on a simulator backend
    sim_result = execute(qc, "local_qasm_simulator")

    # Show the results
    print("simulation: ", sim_result)
    print(sim_result.get_counts(qc))

    # see a list of available remote backends
    remote_backends = available_backends({'local': False, 'simulator': False})

    print("Remote backends: ", remote_backends)
    # Compile and run the Quantum Program on a real device backend
    try:
        best_device = lowest_pending_jobs()
 def test_filter_status_config_dict(self, qe_token, qe_url):
     """Test filtering by dictionary of mixed status/configuration properties"""
     register(qe_token, qe_url)
     filter_ = {'operational': True, 'local': False, 'simulator': True}
     filtered_backends = available_backends(filter_)
     self.assertTrue(filtered_backends)
 def test_filter_config_callable(self, qe_token, qe_url):
     """Test filtering by lambda function on configuration properties"""
     register(qe_token, qe_url)
     filtered_backends = available_backends(lambda x: (not x.configuration()['simulator'] and
                                                       x.configuration()['n_qubits'] > 5))
     self.assertTrue(filtered_backends)
 def test_filter_least_busy(self, qe_token, qe_url):
     """Test filtering by least busy function"""
     register(qe_token, qe_url)
     filtered_backends = least_busy(available_backends())
     self.assertTrue(filtered_backends)
    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("(Local Backends)")
    print(available_backends({'local': True}))

    # runing the job
    sim_result = execute([qc1, qc2], "local_qasm_simulator")

    # 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(Remote Backends)")
    print(available_backends({'local': False}))

    # Compile and run on a real device backend
    try:
Esempio n. 29
0
 def test_filter_least_busy(self, QE_TOKEN, QE_URL):
     """Test filtering by least busy function"""
     register(QE_TOKEN, QE_URL)
     filtered_backends = least_busy(available_backends())
     self.assertTrue(filtered_backends)