Esempio n. 1
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_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 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_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)
Esempio n. 5
0
    def test_qasm_reset_measure(self, qe_token, qe_url):
        """counts from a qasm program with measure and reset in the middle"""
        register(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)
Esempio n. 6
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)
    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)
    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. 9
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
Esempio n. 10
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_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. 12
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
 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_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_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)
    def setUp(self):
        wrapper.register(provider_class=SympyProvider)

        qr = QuantumRegister(2, name="qr2")
        cr = ClassicalRegister(2, name=None)
        qc = QuantumCircuit(qr, cr, name="qc10")
        qc.h(qr[0])
        qc.measure(qr[0], cr[0])
        self.qr_name = qr.name
        self.cr_name = cr.name
        self.circuits = [qc]
Esempio n. 17
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. 18
0
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()
Esempio n. 19
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. 20
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. 21
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. 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)
    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, 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)
Esempio n. 25
0
    def test_qasm(self, qe_token, qe_url):
        """counts from a GHZ state"""
        register(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)
Esempio n. 26
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)
    def setUp(self):
        wrapper.register(provider_class=SympyProvider)

        self.qasm_filename = self._get_resource_path('simple.qasm')
        self.q_circuit = load_qasm_file(self.qasm_filename)
 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)
"""
Example showing how to use QISKit at level 0 (novice).

See level 1 if you would like to understand how to compile

Note: if you have only cloned the QISKit repository but not
used `pip install`, the examples only work from the root directory.
"""

# Import the QISKit modules
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, QISKitError
from qiskit.wrapper import available_backends, execute, register, get_backend

try:
    import Qconfig
    register(Qconfig.APItoken, Qconfig.config['url'])
except:
    print("""WARNING: There's no connection with the API for remote backends.
             Have you initialized a Qconfig.py file with your personal token?
             For now, there's only access to local simulator backends...""")


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'])