Beispiel #1
0
    def test_teleport(self):
        filename = self._get_resource_path('test_teleport.tex')
        QPS_SPECS = {
            "circuits": [{
                "name":
                "teleport",
                "quantum_registers": [{
                    "name": "q",
                    "size": 3
                }],
                "classical_registers": [
                    {
                        "name": "c0",
                        "size": 1
                    },
                    {
                        "name": "c1",
                        "size": 1
                    },
                    {
                        "name": "c2",
                        "size": 1
                    },
                ]
            }]
        }

        qp = QuantumProgram(specs=QPS_SPECS)
        qc = qp.get_circuit("teleport")
        q = qp.get_quantum_register("q")
        c0 = qp.get_classical_register("c0")
        c1 = qp.get_classical_register("c1")
        c2 = qp.get_classical_register("c2")

        # Prepare an initial state
        qc.u3(0.3, 0.2, 0.1, q[0])

        # Prepare a Bell pair
        qc.h(q[1])
        qc.cx(q[1], q[2])

        # Barrier following state preparation
        qc.barrier(q)

        # Measure in the Bell basis
        qc.cx(q[0], q[1])
        qc.h(q[0])
        qc.measure(q[0], c0[0])
        qc.measure(q[1], c1[0])

        # Apply a correction
        qc.z(q[2]).c_if(c0, 1)
        qc.x(q[2]).c_if(c1, 1)
        qc.measure(q[2], c2[0])
        try:
            latex_drawer(qc, filename)
        except Exception:
            if os.path.exists(filename):
                os.remove(filename)
            raise
    def test_local_qasm_simulator_one_shot(self):
        """Test sinlge shot of local simulator .

        If all correct should the quantum state.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc3 = QP_program.create_circuit("qc3", [qr], [cr])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc3.cx(qr[0], qr[1])
        qc3.cx(qr[0], qr[2])
        circuits = ['qc2', 'qc3']
        backend = 'local_qasm_simulator'  # the backend to run on
        shots = 1  # the number of shots in the experiment.
        result = QP_program.execute(circuits, backend=backend, shots=shots,
                                    seed=9)
        quantum_state = np.array([0.70710678+0.j, 0.70710678+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j])
        norm = np.dot(np.conj(quantum_state),
                      result.get_data('qc2')['quantum_state'])
        self.assertAlmostEqual(norm, 1)
        quantum_state = np.array([0.70710678+0.j, 0+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.70710678+0.j])
        norm = np.dot(np.conj(quantum_state),
                      result.get_data('qc3')['quantum_state'])
        self.assertAlmostEqual(norm, 1)
 def test_get_qasm_all_gates(self):
     """Test the get_qasm for more gates, using an specification without names.
     """
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.u1(0.3, qr[0])
     qc.u2(0.2, 0.1, qr[1])
     qc.u3(0.3, 0.2, 0.1, qr[2])
     qc.s(qr[1])
     qc.s(qr[2]).inverse()
     qc.cx(qr[1], qr[2])
     qc.barrier()
     qc.cx(qr[0], qr[1])
     qc.h(qr[0])
     qc.x(qr[2]).c_if(cr, 0)
     qc.y(qr[2]).c_if(cr, 1)
     qc.z(qr[2]).c_if(cr, 2)
     qc.barrier(qr)
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     qc.measure(qr[2], cr[2])
     result = q_program.get_qasm()
     self.assertEqual(len(result), (len(qr.name) * 23 +
                                    len(cr.name) * 7 +
                                    385))
    def test_get_qasm_all_gates(self):
        """Test the get_qasm for more gates.

        If all correct the qasm output should be of a certain lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.u1(0.3, qr[0])
        qc.u2(0.2, 0.1, qr[1])
        qc.u3(0.3, 0.2, 0.1, qr[2])
        qc.s(qr[1])
        qc.s(qr[2]).inverse()
        qc.cx(qr[1], qr[2])
        qc.barrier()
        qc.cx(qr[0], qr[1])
        qc.h(qr[0])
        qc.x(qr[2]).c_if(cr, 0)
        qc.y(qr[2]).c_if(cr, 1)
        qc.z(qr[2]).c_if(cr, 2)
        qc.barrier(qr)
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = QP_program.get_qasm('circuitName')
        self.assertEqual(len(result), 535)
 def test_change_circuit_qobj_after_compile_noname(self):
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc2 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc3 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc2.h(qr[0])
     qc2.cx(qr[0], qr[1])
     qc2.cx(qr[0], qr[2])
     qc3.h(qr)
     qc2.measure(qr, cr)
     qc3.measure(qr, cr)
     circuits = [qc2.name, qc3.name]
     shots = 1024
     backend = 'local_qasm_simulator'
     config = {'seed': 10, 'shots': 1, 'xvals': [1, 2, 3, 4]}
     qobj1 = q_program.compile(circuits, backend=backend, shots=shots, seed=88, config=config)
     qobj1['circuits'][0]['config']['shots'] = 50
     qobj1['circuits'][0]['config']['xvals'] = [1, 1, 1]
     config['shots'] = 1000
     config['xvals'][0] = 'only for qobj2'
     qobj2 = q_program.compile(circuits, backend=backend, shots=shots, seed=88, config=config)
     self.assertTrue(qobj1['circuits'][0]['config']['shots'] == 50)
     self.assertTrue(qobj1['circuits'][1]['config']['shots'] == 1)
     self.assertTrue(qobj1['circuits'][0]['config']['xvals'] == [1, 1, 1])
     self.assertTrue(qobj1['circuits'][1]['config']['xvals'] == [1, 2, 3, 4])
     self.assertTrue(qobj1['config']['shots'] == 1024)
     self.assertTrue(qobj2['circuits'][0]['config']['shots'] == 1000)
     self.assertTrue(qobj2['circuits'][1]['config']['shots'] == 1000)
     self.assertTrue(qobj2['circuits'][0]['config']['xvals'] == [
         'only for qobj2', 2, 3, 4])
     self.assertTrue(qobj2['circuits'][1]['config']['xvals'] == [
         'only for qobj2', 2, 3, 4])
    def test_get_qasm_all_gates(self):
        """Test the get_qasm for more gates.

        If all correct the qasm output should be of a certain lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.u1(0.3, qr[0])
        qc.u2(0.2, 0.1, qr[1])
        qc.u3(0.3, 0.2, 0.1, qr[2])
        qc.s(qr[1])
        qc.s(qr[2]).inverse()
        qc.cx(qr[1], qr[2])
        qc.barrier()
        qc.cx(qr[0], qr[1])
        qc.h(qr[0])
        qc.x(qr[2]).c_if(cr, 0)
        qc.y(qr[2]).c_if(cr, 1)
        qc.z(qr[2]).c_if(cr, 2)
        qc.barrier(qr)
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = QP_program.get_qasm('circuitName')
        self.assertEqual(len(result), 535)
    def test_local_qasm_simulator(self):
        """Test execute.

        If all correct should the data.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc3 = QP_program.create_circuit("qc3", [qr], [cr])
        qc2.h(qr[0])
        qc2.cx(qr[0], qr[1])
        qc2.cx(qr[0], qr[2])
        qc3.h(qr)
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        qc2.measure(qr[1], cr[1])
        qc3.measure(qr[1], cr[1])
        qc2.measure(qr[2], cr[2])
        qc3.measure(qr[2], cr[2])
        circuits = ['qc2', 'qc3']
        shots = 1024  # the number of shots in the experiment.
        backend = 'local_qasm_simulator'
        out = QP_program.execute(circuits, backend=backend, shots=shots,
                                 seed=88)
        results2 = out.get_counts('qc2')
        results3 = out.get_counts('qc3')
        # print(QP_program.get_data('qc3'))
        self.assertEqual(results2, {'000': 518, '111': 506})
        self.assertEqual(results3, {'001': 119, '111': 129, '110': 134,
                                    '100': 117, '000': 129, '101': 126,
                                    '010': 145, '011': 125})
    def test_local_qasm_simulator_one_shot(self):
        """Test sinlge shot of local simulator .

        If all correct should the quantum state.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc3 = QP_program.create_circuit("qc3", [qr], [cr])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc3.cx(qr[0], qr[1])
        qc3.cx(qr[0], qr[2])
        circuits = ['qc2', 'qc3']
        backend = 'local_qasm_simulator'  # the backend to run on
        shots = 1  # the number of shots in the experiment.
        result = QP_program.execute(circuits, backend=backend, shots=shots,
                                    seed=9)
        quantum_state = np.array([0.70710678+0.j, 0.70710678+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j])
        norm = np.dot(np.conj(quantum_state),
                      result.get_data('qc2')['quantum_state'])
        self.assertAlmostEqual(norm, 1)
        quantum_state = np.array([0.70710678+0.j, 0+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.00000000+0.j,
                                  0.00000000+0.j, 0.70710678+0.j])
        norm = np.dot(np.conj(quantum_state),
                      result.get_data('qc3')['quantum_state'])
        self.assertAlmostEqual(norm, 1)
 def test_get_qasm_all_gates(self):
     """Test the get_qasm for more gates, using an specification without names.
     """
     q_program = QuantumProgram(specs=self.qps_specs_nonames)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.u1(0.3, qr[0])
     qc.u2(0.2, 0.1, qr[1])
     qc.u3(0.3, 0.2, 0.1, qr[2])
     qc.s(qr[1])
     qc.s(qr[2]).inverse()
     qc.cx(qr[1], qr[2])
     qc.barrier()
     qc.cx(qr[0], qr[1])
     qc.h(qr[0])
     qc.x(qr[2]).c_if(cr, 0)
     qc.y(qr[2]).c_if(cr, 1)
     qc.z(qr[2]).c_if(cr, 2)
     qc.barrier(qr)
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     qc.measure(qr[2], cr[2])
     result = q_program.get_qasm()
     self.assertEqual(len(result),
                      (len(qr.name) * 23 + len(cr.name) * 7 + 385))
    def test_local_qasm_simulator(self):
        """Test execute.

        If all correct should the data.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc2 = QP_program.create_circuit("qc2", [qr], [cr])
        qc3 = QP_program.create_circuit("qc3", [qr], [cr])
        qc2.h(qr[0])
        qc2.cx(qr[0], qr[1])
        qc2.cx(qr[0], qr[2])
        qc3.h(qr)
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        qc2.measure(qr[1], cr[1])
        qc3.measure(qr[1], cr[1])
        qc2.measure(qr[2], cr[2])
        qc3.measure(qr[2], cr[2])
        circuits = ['qc2', 'qc3']
        shots = 1024  # the number of shots in the experiment.
        backend = 'local_qasm_simulator'
        out = QP_program.execute(circuits, backend=backend, shots=shots,
                                 seed=88)
        results2 = out.get_counts('qc2')
        results3 = out.get_counts('qc3')
        # print(QP_program.get_data('qc3'))
        self.assertEqual(results2, {'000': 518, '111': 506})
        self.assertEqual(results3, {'001': 119, '111': 129, '110': 134,
                                    '100': 117, '000': 129, '101': 126,
                                    '010': 145, '011': 125})
Beispiel #11
0
    def ret(params):
        print("Computing U^{} error with {}: ".format(2**power, params), end='')
        from qiskit import get_backend, execute, QuantumProgram
        from utils.endianness import QRegisterBE, CRegister
        import numpy as np
        import scipy.linalg as la

        def swap(U):
            from copy import deepcopy
            cpy = deepcopy(U)
            cpy[[1,2],:] = cpy[[2,1],:]
            cpy[:,[1,2]] = cpy[:,[2,1]]
            return cpy

        Q_SPECS = {
            "name": "Hamiltonian_error",
            "circuits": [
                {
                    "name": "4x4",
                    "quantum_registers": [
                        {
                            "name": "ctrl",
                            "size": 1
                        },
                        {
                            "name": "qb",
                            "size": 2
                        },
                    ],
                    "classical_registers": [
                    {
                        "name": "classicalX",
                        "size": 2
                    }]
                }
            ],
        }
        Q_program = QuantumProgram(specs=Q_SPECS)

        circuit = Q_program.get_circuit("4x4")
        qb = QRegisterBE(Q_program.get_quantum_register("qb"))
        ctrl = QRegisterBE(Q_program.get_quantum_register("ctrl"))
        classicalX = CRegister(Q_program.get_classical_register('classicalX'))

        circuit.optim_hamil(ctrl[0], qb, params).inverse()

        unitary_sim = get_backend('local_unitary_simulator')
        res = execute([circuit], unitary_sim).result()
        unitary = res.get_unitary()

        A = .25 * np.array([[15, 9, 5, -3],
                            [9, 15, 3, -5],
                            [5, 3, 15, -9],
                            [-3, -5, -9, 15]])
        expA = swap(la.expm(-1.j * A * (2**power) * 2 * np.pi / 16))
        unit = unitary[1::2, 1::2]
        err = la.norm(unit - expA)
        print(err)
        return err
 def test_execute_one_circuit_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qc = QP_program.get_circuit("circuitName")
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc.h(qr[1])
     qc.measure(qr[0], cr[0])
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     # print(backend)
     result = QP_program.execute(['circuitName'], backend=backend,
                                 shots=shots, max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_compile_program_noname(self):
     """Test compile with a no name.
     """
     q_program = QuantumProgram(specs=self.qps_specs_nonames)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.h(qr[0])
     qc.cx(qr[0], qr[1])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     out = q_program.compile()
     self.log.info(out)
     self.assertIsInstance(out, Qobj)
 def test_get_execution_list_noname(self):
     """Test get_execution_list for circuits without name.
     """
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.h(qr[0])
     qc.cx(qr[0], qr[1])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     qobj = q_program.compile()
     result = q_program.get_execution_list(qobj, print_func=self.log.info)
     self.assertEqual(len(result), 1)
 def test_execute_one_circuit_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qc = QP_program.get_circuit("circuitName")
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc.h(qr[1])
     qc.measure(qr[0], cr[0])
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     # print(backend)
     result = QP_program.execute(['circuitName'], backend=backend,
                                 shots=shots, max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_compile_program_noname(self):
     """Test compile with a no name.
     """
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.h(qr[0])
     qc.cx(qr[0], qr[1])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     out = q_program.compile()
     self.log.info(out)
     self.assertEqual(len(out), 3)
Beispiel #17
0
 def test_compile_program_noname(self):
     """Test compile with a no name.
     """
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.h(qr[0])
     qc.cx(qr[0], qr[1])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     out = q_program.compile()
     self.log.info(out)
     self.assertEqual(len(out), 3)
 def test_get_execution_list_noname(self):
     """Test get_execution_list for circuits without name.
     """
     q_program = QuantumProgram(specs=self.qps_specs_nonames)
     qc = q_program.get_circuit()
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc.h(qr[0])
     qc.cx(qr[0], qr[1])
     qc.measure(qr[0], cr[0])
     qc.measure(qr[1], cr[1])
     qobj = q_program.compile()
     result = q_program.get_execution_list(qobj, print_func=self.log.info)
     self.assertEqual(len(result), 1)
    def test_get_register_and_circuit(self):
        """Test get_quantum_registers, get_classical_registers, and get_circuit.

        If all is correct we get a object intstance of QuantumCircuit,
        QuantumRegister, ClassicalRegister

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        self.assertIsInstance(qc, QuantumCircuit)
        self.assertIsInstance(qr, QuantumRegister)
        self.assertIsInstance(cr, ClassicalRegister)
 def test_execute_several_circuits_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc2 = QP_program.create_circuit("qc2", [qr], [cr])
     qc3 = QP_program.create_circuit("qc3", [qr], [cr])
     qc2.h(qr[0])
     qc3.h(qr[0])
     qc2.measure(qr[0], cr[0])
     qc3.measure(qr[0], cr[0])
     circuits = ['qc2', 'qc3']
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     result = QP_program.execute(circuits, backend=backend, shots=shots,
                                 max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
    def test_get_register_and_circuit(self):
        """Test get_quantum_registers, get_classical_registers, and get_circuit.

        If all is correct we get a object intstance of QuantumCircuit,
        QuantumRegister, ClassicalRegister

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        self.assertIsInstance(qc, QuantumCircuit)
        self.assertIsInstance(qr, QuantumRegister)
        self.assertIsInstance(cr, ClassicalRegister)
 def test_execute_several_circuits_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc2 = QP_program.create_circuit("qc2", [qr], [cr])
     qc3 = QP_program.create_circuit("qc3", [qr], [cr])
     qc2.h(qr[0])
     qc3.h(qr[0])
     qc2.measure(qr[0], cr[0])
     qc3.measure(qr[0], cr[0])
     circuits = ['qc2', 'qc3']
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     result = QP_program.execute(circuits, backend=backend, shots=shots,
                                 max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_change_circuit_qobj_after_compile_noname(self):
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qr = q_program.get_quantum_register()
     cr = q_program.get_classical_register()
     qc2 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc3 = q_program.create_circuit(qregisters=[qr], cregisters=[cr])
     qc2.h(qr[0])
     qc2.cx(qr[0], qr[1])
     qc2.cx(qr[0], qr[2])
     qc3.h(qr)
     qc2.measure(qr, cr)
     qc3.measure(qr, cr)
     circuits = [qc2.name, qc3.name]
     shots = 1024
     backend = 'local_qasm_simulator'
     config = {'seed': 10, 'shots': 1, 'xvals': [1, 2, 3, 4]}
     qobj1 = q_program.compile(circuits,
                               backend=backend,
                               shots=shots,
                               seed=88,
                               config=config)
     qobj1 = qobj1.as_dict()
     qobj1['circuits'][0]['config']['shots'] = 50
     qobj1['circuits'][0]['config']['xvals'] = [1, 1, 1]
     config['shots'] = 1000
     config['xvals'][0] = 'only for qobj2'
     qobj2 = q_program.compile(circuits,
                               backend=backend,
                               shots=shots,
                               seed=88,
                               config=config)
     qobj2 = qobj2.as_dict()
     self.assertTrue(qobj1['circuits'][0]['config']['shots'] == 50)
     self.assertTrue(qobj1['circuits'][1]['config']['shots'] == 1)
     self.assertTrue(qobj1['circuits'][0]['config']['xvals'] == [1, 1, 1])
     self.assertTrue(
         qobj1['circuits'][1]['config']['xvals'] == [1, 2, 3, 4])
     self.assertTrue(qobj1['config']['shots'] == 1024)
     self.assertTrue(qobj2['circuits'][0]['config']['shots'] == 1000)
     self.assertTrue(qobj2['circuits'][1]['config']['shots'] == 1000)
     self.assertTrue(qobj2['circuits'][0]['config']['xvals'] ==
                     ['only for qobj2', 2, 3, 4])
     self.assertTrue(qobj2['circuits'][1]['config']['xvals'] ==
                     ['only for qobj2', 2, 3, 4])
    def test_compile_program(self):
        """Test compile_program.

        If all correct should return COMPLETED.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'test'
        coupling_map = None
        out = QP_program.compile(['circuitName'], backend=backend,
                                 coupling_map=coupling_map, qobjid='cooljob')
        # print(out)
        self.assertEqual(len(out), 3)
    def test_compile_program(self):
        """Test compile_program.

        If all correct should return COMPLETED.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'test'
        coupling_map = None
        out = QP_program.compile(['circuitName'], backend=backend,
                                 coupling_map=coupling_map, qobjid='cooljob')
        # print(out)
        self.assertEqual(len(out), 3)
    def test_get_execution_list(self):
        """Test get_execution_list.

        If all correct should return {'local_qasm_simulator': ['circuitName']}.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'local_qasm_simulator'
        coupling_map = None
        qobj = QP_program.compile(['circuitName'], backend=backend,
                                  coupling_map=coupling_map, qobjid="cooljob")
        result = QP_program.get_execution_list(qobj)
        # print(result)
        self.assertEqual(result, ['circuitName'])
    def test_get_compiled_qasm(self):
        """Test get_compiled_qasm.

        If all correct should return lenght  dictionary.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'local_qasm_simulator'
        coupling_map = None
        qobj = QP_program.compile(['circuitName'], backend=backend,
                                  coupling_map=coupling_map)
        result = QP_program.get_compiled_qasm(qobj, 'circuitName',)
        # print(result)
        self.assertEqual(len(result), 184)
    def test_get_compiled_qasm(self):
        """Test get_compiled_qasm.

        If all correct should return lenght  dictionary.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'local_qasm_simulator'
        coupling_map = None
        qobj = QP_program.compile(['circuitName'], backend=backend,
                                  coupling_map=coupling_map)
        result = QP_program.get_compiled_qasm(qobj, 'circuitName',)
        # print(result)
        self.assertEqual(len(result), 184)
    def test_get_execution_list(self):
        """Test get_execution_list.

        If all correct should return {'local_qasm_simulator': ['circuitName']}.
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        backend = 'local_qasm_simulator'
        coupling_map = None
        qobj = QP_program.compile(['circuitName'], backend=backend,
                                  coupling_map=coupling_map, qobjid="cooljob")
        result = QP_program.get_execution_list(qobj)
        # print(result)
        self.assertEqual(result, ['circuitName'])
    def test_get_qasm(self):
        """Test the get_qasm.

        If all correct the qasm output should be of a certain lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.cx(qr[1], qr[2])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = QP_program.get_qasm("circuitName")
        self.assertEqual(len(result), 212)
    def test_get_qasm(self):
        """Test the get_qasm.

        If all correct the qasm output should be of a certain lenght

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)
        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")
        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.cx(qr[1], qr[2])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = QP_program.get_qasm("circuitName")
        self.assertEqual(len(result), 212)
    def test_get_qasm_noname(self):
        """Test the get_qasm using an specification without names.
        """
        q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
        qc = q_program.get_circuit()

        qrn = list(q_program.get_quantum_register_names())
        self.assertEqual(len(qrn), 1)
        qr = q_program.get_quantum_register(qrn[0])

        crn = list(q_program.get_classical_register_names())
        self.assertEqual(len(crn), 1)
        cr = q_program.get_classical_register(crn[0])

        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.cx(qr[1], qr[2])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = q_program.get_qasm()
        self.assertEqual(len(result), len(qrn[0]) * 9 + len(crn[0]) * 4 + 147)
    def test_get_qasm_noname(self):
        """Test the get_qasm using an specification without names.
        """
        q_program = QuantumProgram(specs=self.qps_specs_nonames)
        qc = q_program.get_circuit()

        qrn = list(q_program.get_quantum_register_names())
        self.assertEqual(len(qrn), 1)
        qr = q_program.get_quantum_register(qrn[0])

        crn = list(q_program.get_classical_register_names())
        self.assertEqual(len(crn), 1)
        cr = q_program.get_classical_register(crn[0])

        qc.h(qr[0])
        qc.cx(qr[0], qr[1])
        qc.cx(qr[1], qr[2])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        qc.measure(qr[2], cr[2])
        result = q_program.get_qasm()
        self.assertEqual(len(result), len(qrn[0]) * 9 + len(crn[0]) * 4 + 147)
    def test_save(self):
        """
        Save a Quantum Program in Json file
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")

        qc.u3(0.3, 0.2, 0.1, qr[0])
        qc.h(qr[1])
        qc.cx(qr[1], qr[2])
        qc.barrier()
        qc.cx(qr[0], qr[1])
        qc.h(qr[0])
        qc.z(qr[2]).c_if(cr, 1)
        qc.x(qr[2]).c_if(cr, 1)
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])

        result = QP_program.save("./test/python/test_save.json", beauty=True)

        self.assertEqual(result['status'], 'Done')
    def test_save(self):
        """
        Save a Quantum Program in Json file
        """
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_register("qname")
        cr = QP_program.get_classical_register("cname")

        qc.u3(0.3, 0.2, 0.1, qr[0])
        qc.h(qr[1])
        qc.cx(qr[1], qr[2])
        qc.barrier()
        qc.cx(qr[0], qr[1])
        qc.h(qr[0])
        qc.z(qr[2]).c_if(cr, 1)
        qc.x(qr[2]).c_if(cr, 1)
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])

        result = QP_program.save("./test/python/test_save.json", beauty=True)

        self.assertEqual(result['status'], 'Done')
            ],
            'classical_registers': [
                {
                    'name': 'ans',
                    'size': n
                },
            ]
        }]
    }

    qp = QuantumProgram(specs=QPS_SPECS)
    qc = qp.get_circuit('grover')
    f_in = qp.get_quantum_register('f_in')
    f_out = qp.get_quantum_register('f_out')
    aux = qp.get_quantum_register('aux')
    ans = qp.get_classical_register('ans')

    input_state(qc, f_in, f_out, n)
    # Apply two full iterations
    black_box_u_f(qc, f_in, f_out, aux, n, exactly_1_3_sat_formula)
    inversion_about_average(qc, f_in, n)
    black_box_u_f(qc, f_in, f_out, aux, n, exactly_1_3_sat_formula)
    inversion_about_average(qc, f_in, n)
    # Measure the output register in the computational basis
    for j in range(n):
        qc.measure(f_in[j], ans[j])

    # Execute circuit
    result = qp.execute(['grover'],
                        backend='local_qasm_simulator',
                        coupling_map=None,
 def test_get_classical_register_noname(self):
     q_program = QuantumProgram(specs=self.qps_specs_nonames)
     cr = q_program.get_classical_register()
     self.assertIsInstance(cr, ClassicalRegister)
Beispiel #38
0
        "classical_registers": [
            {
                "name": "ans",
                "size": n + 1
            },
        ]
    }]
}

qp = QuantumProgram(specs=QPS_SPECS)
qc = qp.get_circuit("rippleadd")
a = qp.get_quantum_register("a")
b = qp.get_quantum_register("b")
cin = qp.get_quantum_register("cin")
cout = qp.get_quantum_register("cout")
ans = qp.get_classical_register("ans")


def majority(p, a, b, c):
    """Majority gate."""
    p.cx(c, b)
    p.cx(c, a)
    p.ccx(a, b, c)


def unmajority(p, a, b, c):
    """Unmajority gate."""
    p.ccx(a, b, c)
    p.cx(c, a)
    p.cx(a, b)
 def test_get_classical_register_noname(self):
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     cr = q_program.get_classical_register()
     self.assertIsInstance(cr, ClassicalRegister)
    def test_example_multiple_compile(self):
        """Test a toy example compiling multiple circuits.

        Pass if the results are correct.
        """
        coupling_map = {0: [1, 2],
                        1: [2],
                        2: [],
                        3: [2, 4],
                        4: [2]}
        QPS_SPECS = {
            "circuits": [{
                "name": "ghz",
                "quantum_registers": [{
                    "name": "q",
                    "size": 5
                }],
                "classical_registers": [{
                    "name": "c",
                    "size": 5}
                ]}, {
                "name": "bell",
                "quantum_registers": [{
                    "name": "q",
                    "size": 5
                }],
                "classical_registers": [{
                    "name": "c",
                    "size": 5
                }]}
            ]
        }
        qp = QuantumProgram(specs=QPS_SPECS)
        ghz = qp.get_circuit("ghz")
        bell = qp.get_circuit("bell")
        q = qp.get_quantum_register("q")
        c = qp.get_classical_register("c")
        # Create a GHZ state
        ghz.h(q[0])
        for i in range(4):
            ghz.cx(q[i], q[i+1])
        # Insert a barrier before measurement
        ghz.barrier()
        # Measure all of the qubits in the standard basis
        for i in range(5):
            ghz.measure(q[i], c[i])
        # Create a Bell state
        bell.h(q[0])
        bell.cx(q[0], q[1])
        bell.barrier()
        bell.measure(q[0], c[0])
        bell.measure(q[1], c[1])
        qp.set_api(API_TOKEN, URL)
        bellobj = qp.compile(["bell"], backend='local_qasm_simulator',
                             shots=2048, seed=10)
        ghzobj = qp.compile(["ghz"], backend='local_qasm_simulator',
                            shots=2048, coupling_map=coupling_map,
                            seed=10)
        bellresult = qp.run(bellobj)
        ghzresult = qp.run(ghzobj)
        print(bellresult.get_counts("bell"))
        print(ghzresult.get_counts("ghz"))
        self.assertEqual(bellresult.get_counts("bell"),
                         {'00000': 1034, '00011': 1014})
        self.assertEqual(ghzresult.get_counts("ghz"),
                         {'00000': 1047, '11111': 1001})
    "name": "Program-tutorial",
    "circuits": [{
        "name": "Circuit",
        "quantum_registers": [{
            "name": "qr",
            "size": 4
        }],
        "classical_registers": [{
            "name": "cr",
            "size": 4
        }]}],
}
Q_program = QuantumProgram(specs=Q_SPECS)
circuit = Q_program.get_circuit("Circuit")
quantum_r = Q_program.get_quantum_register("qr")
classical_r = Q_program.get_classical_register('cr')

circuit.h(quantum_r[0])
circuit.rx(0, quantum_r[0])

circuit.cx(quantum_r[0], quantum_r[1])
circuit.cx(quantum_r[0], quantum_r[1])

circuit.h(quantum_r[0])

circuit.cx(quantum_r[0], quantum_r[1])
composite_gate_1 = CompositeGate("composite1", [],
                                 [quantum_r[x] for x in range(4)])

composite_gate_1._attach(CnotGate(quantum_r[0], quantum_r[1]))
circuit._attach(composite_gate_1)
Beispiel #42
0
    for j in range(n):
        circ.h(q[j])
        circ.u1(math.pi/float(2**(j)), q[j]).inverse()


def qft(circ, q, n):
    """n-qubit QFT on q in circ."""
    for j in range(n):
        for k in range(j):
            circ.cu1(math.pi/float(2**(j-k)), q[j], q[k])
        circ.h(q[j])


qp = QuantumProgram(specs=QPS_SPECS)
q = qp.get_quantum_register("q")
c = qp.get_classical_register("c")

qft3 = qp.get_circuit("qft3")
qft4 = qp.get_circuit("qft4")
qft5 = qp.get_circuit("qft5")

input_state(qft3, q, 3)
qft3.barrier()
qft(qft3, q, 3)
qft3.barrier()
for j in range(3):
    qft3.measure(q[j], c[j])

input_state(qft4, q, 4)
qft4.barrier()
qft(qft4, q, 4)
Beispiel #43
0
from qiskit import QuantumProgram
import Qconfig

qp = QuantumProgram()
qp.set_api(
    'a2c1e632e1d80403ae002b7fde0fb7dfe4afd5a4093292c24c75a65060ed81c16644cf1d7a5b5a4075c08e3530ee8e125d0405b910e73ee79d2441c990145be4',
    'https://quantumexperience.ng.bluemix.net/api'
)  # set the APIToken and API url
# set up registers and program
qr = qp.create_quantum_register('qr', 2)
cr = qp.create_classical_register('cr', 2)
qc = qp.create_circuit('my_test', [qr], [cr])

circuit = qp.get_circuit('my_test')
quantum_register = qp.get_quantum_register('qr')
classical_register = qp.get_classical_register('cr')

circuit.x(quantum_register[0])
#circuit.y(quantum_register[1])

circuit.cx(quantum_register[0], quantum_register[1])
circuit.barrier(qr)

# measure
for j in range(2):
    circuit.measure(qr[j], cr[j])

# run and get results
results = qp.execute(["my_test"],
                     backend='ibmqx_qasm_simulator',
                     wait=2,
    def test_example_swap_bits(self):
        """Test a toy example swapping a set bit around.

        Uses the mapper. Pass if results are correct.
        """
        backend = "ibmqx_qasm_simulator"
        coupling_map = {0: [1, 8], 1: [2, 9], 2: [3, 10], 3: [4, 11], 4: [5, 12],
                        5: [6, 13], 6: [7, 14], 7: [15], 8: [9], 9: [10], 10: [11],
                        11: [12], 12: [13], 13: [14], 14: [15]}
        def swap(qc, q0, q1):
            """Swap gate."""
            qc.cx(q0, q1)
            qc.cx(q1, q0)
            qc.cx(q0, q1)
        n = 3  # make this at least 3
        QPS_SPECS = {
            "circuits": [{
                "name": "swapping",
                "quantum_registers": [{
                    "name": "q",
                    "size": n},
                    {"name": "r",
                     "size": n}
                ],
                "classical_registers": [
                    {"name": "ans",
                     "size": 2*n},
                ]
            }]
        }
        qp = QuantumProgram(specs=QPS_SPECS)
        qp.set_api(API_TOKEN, URL)
        if backend not in qp.online_simulators():
            return
        qc = qp.get_circuit("swapping")
        q = qp.get_quantum_register("q")
        r = qp.get_quantum_register("r")
        ans = qp.get_classical_register("ans")
        # Set the first bit of q
        qc.x(q[0])
        # Swap the set bit
        swap(qc, q[0], q[n-1])
        swap(qc, q[n-1], r[n-1])
        swap(qc, r[n-1], q[1])
        swap(qc, q[1], r[1])
        # Insert a barrier before measurement
        qc.barrier()
        # Measure all of the qubits in the standard basis
        for j in range(n):
            qc.measure(q[j], ans[j])
            qc.measure(r[j], ans[j+n])
        # First version: no mapping
        result = qp.execute(["swapping"], backend=backend,
                            coupling_map=None, shots=1024,
                            seed=14)
        self.assertEqual(result.get_counts("swapping"),
                         {'010000': 1024})
        # Second version: map to coupling graph
        result = qp.execute(["swapping"], backend=backend,
                            coupling_map=coupling_map, shots=1024,
                            seed=14)
        self.assertEqual(result.get_counts("swapping"),
                         {'010000': 1024})
def period(a, N):
    global Ran_Quantum_period_finding
    Ran_Quantum_period_finding = 1
    # Create the first QuantumProgram object instance.
    qp = QuantumProgram()
    #qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
    # TO DO : generalize the number of qubits and give proper security against rogue input.
    # Create the first Quantum Register called "qr" with 12 qubits
    qr = qp.create_quantum_register('qr', 5)
    # Create your first Classical Register  called "cr" with 12 bits
    cr = qp.create_classical_register('cr', 3)

    # Create the first Quantum Circuit called "qc" involving your Quantum Register "qr"
    # and the Classical Register "cr"
    qc = qp.create_circuit('Period_Finding', [qr], [cr])

    # Get the circuit and the registers by name
    Shor1 = qp.get_circuit('Period_Finding')
    Q_reg = qp.get_quantum_register('qr')
    C_reg = qp.get_classical_register('cr')

    # Create the circuit for period finding
    # Initialize qr[0] to |1>
    Shor1.x(Q_reg[0])

    # Step one : apply a**4 mod 15
    Shor1.h(Q_reg[4])
    # Controlled Identity on the remaining 4 qubits. Which is equivalent to doing nothing
    Shor1.h(Q_reg[4])
    Shor1.measure(Q_reg[4], C_reg[0])
    # Reinitialize to |0>
    Shor1.reset(Q_reg[4])

    # Step two : apply a**2 mod 15
    Shor1.h(Q_reg[4])
    # Controlled unitary. Apply a mod 15 twice.
    for k in range(2):
        cmod(qp, 'Period_Finding', 'qr', a)
    if C_reg[0] == 1:
        Shor1.u1(pi / 2.0, Q_reg[4])
    Shor1.h(Q_reg[4])
    Shor1.measure(Q_reg[4], C_reg[1])
    # Reinitialize to |0>
    Shor1.reset(Q_reg[4])

    # Step three : apply 11 mod 15
    Shor1.h(Q_reg[4])
    # Controlled unitary. Apply a mod 15
    cmod(qp, 'Period_Finding', 'qr', a)
    # Feed forward and measure
    if C_reg[1] == 1:
        Shor1.u1(pi / 2.0, Q_reg[4])
    if C_reg[0] == 1:
        Shor1.u1(pi / 4.0, Q_reg[4])
    Shor1.h(Q_reg[4])
    Shor1.measure(Q_reg[4], C_reg[2])

    # Run the circuit
    #qp.set_api(Qconfig.APItoken, Qconfig.config['url']) # set the APIToken and API url
    simulate = qp.execute(["Period_Finding"],
                          backend="local_qasm_simulator",
                          shots=1,
                          timeout=500)
    simulate.get_counts("Period_Finding")
    #print(simulate)
    data = simulate.get_counts("Period_Finding")
    #print(data)
    data = list(data.keys())
    #print(data)
    r = int(data[0])
    #print(r)
    l = gcd(2**3, r)
    #print(l)
    r = int((2**3) / l)
    #print(r)
    return r
Beispiel #46
0
 def test_get_classical_register_noname(self):
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     cr = q_program.get_classical_register()
     self.assertIsInstance(cr, ClassicalRegister)
    def test_example_swap_bits(self):
        """Test a toy example swapping a set bit around.

        Uses the mapper. Pass if results are correct.
        """
        backend = "ibmqx_qasm_simulator"
        coupling_map = {0: [1, 8], 1: [2, 9], 2: [3, 10], 3: [4, 11], 4: [5, 12],
                        5: [6, 13], 6: [7, 14], 7: [15], 8: [9], 9: [10], 10: [11],
                        11: [12], 12: [13], 13: [14], 14: [15]}
        def swap(qc, q0, q1):
            """Swap gate."""
            qc.cx(q0, q1)
            qc.cx(q1, q0)
            qc.cx(q0, q1)
        n = 3  # make this at least 3
        QPS_SPECS = {
            "circuits": [{
                "name": "swapping",
                "quantum_registers": [{
                    "name": "q",
                    "size": n},
                    {"name": "r",
                     "size": n}
                ],
                "classical_registers": [
                    {"name": "ans",
                     "size": 2*n},
                ]
            }]
        }
        qp = QuantumProgram(specs=QPS_SPECS)
        qp.set_api(API_TOKEN, URL)
        if backend not in qp.online_simulators():
            return
        qc = qp.get_circuit("swapping")
        q = qp.get_quantum_register("q")
        r = qp.get_quantum_register("r")
        ans = qp.get_classical_register("ans")
        # Set the first bit of q
        qc.x(q[0])
        # Swap the set bit
        swap(qc, q[0], q[n-1])
        swap(qc, q[n-1], r[n-1])
        swap(qc, r[n-1], q[1])
        swap(qc, q[1], r[1])
        # Insert a barrier before measurement
        qc.barrier()
        # Measure all of the qubits in the standard basis
        for j in range(n):
            qc.measure(q[j], ans[j])
            qc.measure(r[j], ans[j+n])
        # First version: no mapping
        result = qp.execute(["swapping"], backend=backend,
                            coupling_map=None, shots=1024,
                            seed=14)
        self.assertEqual(result.get_counts("swapping"),
                         {'010000': 1024})
        # Second version: map to coupling graph
        result = qp.execute(["swapping"], backend=backend,
                            coupling_map=coupling_map, shots=1024,
                            seed=14)
        self.assertEqual(result.get_counts("swapping"),
                         {'010000': 1024})
    def test_example_multiple_compile(self):
        """Test a toy example compiling multiple circuits.

        Pass if the results are correct.
        """
        coupling_map = {0: [1, 2],
                        1: [2],
                        2: [],
                        3: [2, 4],
                        4: [2]}
        QPS_SPECS = {
            "circuits": [{
                "name": "ghz",
                "quantum_registers": [{
                    "name": "q",
                    "size": 5
                }],
                "classical_registers": [{
                    "name": "c",
                    "size": 5}
                ]}, {
                "name": "bell",
                "quantum_registers": [{
                    "name": "q",
                    "size": 5
                }],
                "classical_registers": [{
                    "name": "c",
                    "size": 5
                }]}
            ]
        }
        qp = QuantumProgram(specs=QPS_SPECS)
        ghz = qp.get_circuit("ghz")
        bell = qp.get_circuit("bell")
        q = qp.get_quantum_register("q")
        c = qp.get_classical_register("c")
        # Create a GHZ state
        ghz.h(q[0])
        for i in range(4):
            ghz.cx(q[i], q[i+1])
        # Insert a barrier before measurement
        ghz.barrier()
        # Measure all of the qubits in the standard basis
        for i in range(5):
            ghz.measure(q[i], c[i])
        # Create a Bell state
        bell.h(q[0])
        bell.cx(q[0], q[1])
        bell.barrier()
        bell.measure(q[0], c[0])
        bell.measure(q[1], c[1])
        qp.set_api(API_TOKEN, URL)
        bellobj = qp.compile(["bell"], backend='local_qasm_simulator',
                             shots=2048, seed=10)
        ghzobj = qp.compile(["ghz"], backend='local_qasm_simulator',
                            shots=2048, coupling_map=coupling_map,
                            seed=10)
        bellresult = qp.run(bellobj)
        ghzresult = qp.run(ghzobj)
        print(bellresult.get_counts("bell"))
        print(ghzresult.get_counts("ghz"))
        self.assertEqual(bellresult.get_counts("bell"),
                         {'00000': 1034, '00011': 1014})
        self.assertEqual(ghzresult.get_counts("ghz"),
                         {'00000': 1047, '11111': 1001})
            "size": 3
        }],
        "classical_registers": [
            {"name": "c0",
             "size": 1},
            {"name": "c1",
             "size": 1},
            {"name": "c2",
             "size": 1},
        ]}]
}

qp = QuantumProgram(specs=QPS_SPECS)
qc = qp.get_circuit("teleport")
q = qp.get_quantum_register("q")
c0 = qp.get_classical_register("c0")
c1 = qp.get_classical_register("c1")
c2 = qp.get_classical_register("c2")

# Prepare an initial state
qc.u3(0.3, 0.2, 0.1, q[0])

# Prepare a Bell pair
qc.h(q[1])
qc.cx(q[1], q[2])

# Barrier following state preparation
qc.barrier(q)

# Measure in the Bell basis
qc.cx(q[0], q[1])
Beispiel #50
0
            'size': 2
        }],
        'classical_registers': [{
            'name': 'c',
            'size': 2
        }]
    }],
}

Q_program = QuantumProgram(specs=QPS_SPECS)
Q_program.set_api(Qconfig.APItoken, Qconfig.config['url'])

# quantum circuit to make Bell state
bell = Q_program.get_circuit('bell')
q = Q_program.get_quantum_register('q')
c = Q_program.get_classical_register('c')

bell.h(q[0])
bell.cx(q[0], q[1])

# quantum circuit to measure q in standard basis
measureZZ = Q_program.create_circuit('measureZZ', [q], [c])
measureZZ.measure(q[0], c[0])
measureZZ.measure(q[1], c[1])

# quantum circuit to measure q in superposition basis
measureXX = Q_program.create_circuit('measureXX', [q], [c])
measureXX.h(q[0])
measureXX.h(q[1])
measureXX.measure(q[0], c[0])
measureXX.measure(q[1], c[1])
Beispiel #51
0
        "name": "ghz",
        "quantum_registers": [{
            "name": "q",
            "size": 5
        }],
        "classical_registers": [{
            "name": "c",
            "size": 5
        }]
    }]
}

qp = QuantumProgram(specs=QPS_SPECS)
qc = qp.get_circuit("ghz")
q = qp.get_quantum_register("q")
c = qp.get_classical_register("c")

# Create a GHZ state
qc.h(q[0])
for i in range(4):
    qc.cx(q[i], q[i + 1])
# Insert a barrier before measurement
qc.barrier()
# Measure all of the qubits in the standard basis
for i in range(5):
    qc.measure(q[i], c[i])

###############################################################
# Set up the API and execute the program.
###############################################################
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
Beispiel #52
0
            {
                "name": "c1",
                "size": 1
            },
            {
                "name": "c2",
                "size": 1
            },
        ]
    }]
}

qp = QuantumProgram(specs=QPS_SPECS)
qc = qp.get_circuit("teleport")
q = qp.get_quantum_register("q")
c0 = qp.get_classical_register("c0")
c1 = qp.get_classical_register("c1")
c2 = qp.get_classical_register("c2")

# Prepare an initial state
qc.u3(0.3, 0.2, 0.1, q[0])

# Prepare a Bell pair
qc.h(q[1])
qc.cx(q[1], q[2])

# Barrier following state preparation
qc.barrier(q)

# Measure in the Bell basis
qc.cx(q[0], q[1])
Beispiel #53
0
            {"name": "cout",
             "size": 1}
        ],
        "classical_registers": [
            {"name": "ans",
             "size": n + 1},
        ]}]
}

qp = QuantumProgram(specs=QPS_SPECS)
qc = qp.get_circuit("rippleadd")
a = qp.get_quantum_register("a")
b = qp.get_quantum_register("b")
cin = qp.get_quantum_register("cin")
cout = qp.get_quantum_register("cout")
ans = qp.get_classical_register("ans")


def majority(p, a, b, c):
    """Majority gate."""
    p.cx(c, b)
    p.cx(c, a)
    p.ccx(a, b, c)


def unmajority(p, a, b, c):
    """Unmajority gate."""
    p.ccx(a, b, c)
    p.cx(c, a)
    p.cx(a, b)