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)
Пример #2
0
    def test_run_program(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc2 = QP_program.create_circuit("qc2", ["qname"], ["cname"])
        qc3 = QP_program.create_circuit("qc3", ["qname"], ["cname"])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        circuits = ['qc2', 'qc3']

        device = 'simulator'  # the device to run on
        shots = 1024  # the number of shots in the experiment.
        credits = 3
        coupling_map = None

        apiconnection = QP_program.set_api(API_TOKEN, URL)
        QP_program.compile(circuits, device, shots, credits, coupling_map)
        result = QP_program.run()
        # print(QP_program())
        print(result)
        # TODO: Revire result
        self.assertEqual(result['status'], 'COMPLETED')
Пример #3
0
def main():
    qp = QuantumProgram()
    #Create 1 qubit
    quantum_r = qp.create_quantum_register("qr",1)
    #Create 1 classical register
    classical_r = qp.create_classical_register("cr",1)
    #Create a circuit
    qp.create_circuit("Circuit", [quantum_r], [classical_r])
    #Get the circuit by name
    circuit = qp.get_circuit('Circuit')
    #enable logging
    qp.enable_logs(logging.DEBUG);
    #pauliX gate
    circuit.x(quantum_r[0])
    #measure gate from qubit 0 to classical bit 0
    circuit.measure(quantum_r[0], classical_r[0])

    #backend simulator
    backend = 'local_qasm_simulator'
    #circuits to execute
    circuits = ['Circuit']
    #Compile the program
    qobj = qp.compile(circuits, backend)
    #run simulator
    result = qp.run(qobj, timeout=240)
    #Show result counts
    print(str(result.get_counts('Circuit')))
    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)
Пример #5
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
Пример #6
0
 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, 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))
Пример #8
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
Пример #9
0
    def test_print_program(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc.h(qr[1])
        result = QP_program.get_qasm("circuitName")
        self.assertEqual(len(result), 78)
Пример #10
0
 def test_get_individual_components(self):
     """
     Get the program componentes, like Circuits and Registers
     """
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qc = QP_program.get_circuit("circuitName")
     qr = QP_program.get_quantum_registers("qname")
     cr = QP_program.get_classical_registers("cname")
     self.assertIsInstance(qc, QuantumCircuit)
     self.assertIsInstance(qr, QuantumRegister)
     self.assertIsInstance(cr, ClassicalRegister)
Пример #11
0
    def test_compile_program(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc.h(qr[0])
        qc.h(qr[0])
        qc.measure(qr[0], cr[0])

        device = 'ibmqx2'
        shots = 1024
        credits = 3
        coupling_map = None

        result = QP_program.compile(['circuitName'], device, coupling_map)

        to_test = QP_program.get_circuit('circuitName')

        self.assertEqual(len(to_test.qasm()), 120)
 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)
Пример #13
0
 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)
Пример #14
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)
Пример #15
0
 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)
Пример #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)
Пример #18
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.assertIsInstance(out, Qobj)
Пример #19
0
    def test_new_compile(self):
        QP_program = QuantumProgram()
        device = 'local_qasm_simulator'  # the device to run on
        shots = 1  # the number of shots in the experiment.
        credits = 3
        coupling_map = None
        QP_program.load_qasm("circuit-dev", "test.qasm")
        circuits = ["circuit-dev"]

        result = QP_program.compile(circuits, device, shots, credits,
                                    coupling_map)
        to_check = QP_program.get_circuit("circuit-dev")

        self.assertEqual(len(to_check.qasm()), 1569)
    def test_load_qasm_file(self):
        """Test load_qasm_file and get_circuit.

        If all is correct we should get the qasm file loaded in QASM_FILE_PATH

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        name = QP_program.load_qasm_file(QASM_FILE_PATH, name="",
                                         verbose=False)
        result = QP_program.get_circuit(name)
        to_check = result.qasm()
        # print(to_check)
        self.assertEqual(len(to_check), 554)
    def test_load_qasm_file(self):
        """Test load_qasm_file and get_circuit.

        If all is correct we should get the qasm file loaded in QASM_FILE_PATH

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        name = QP_program.load_qasm_file(QASM_FILE_PATH, name="",
                                         verbose=False)
        result = QP_program.get_circuit(name)
        to_check = result.qasm()
        # print(to_check)
        self.assertEqual(len(to_check), 554)
Пример #22
0
    def test_contact_multiple_horizontal_circuits(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc2 = QP_program.create_circuit(name="qc2",
                                        qregisters=["qname"],
                                        cregisters=["cname"])
        qc3 = QP_program.create_circuit("qc3", ["qname"], ["cname"])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        qc_result = qc2 + qc3
        self.assertIsInstance(qc_result, QuantumCircuit)
    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_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_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)
Пример #26
0
    def test_execute_program_simulator_online(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc2 = QP_program.create_circuit("qc2", ["qname"], ["cname"])
        qc3 = QP_program.create_circuit("qc3", ["qname"], ["cname"])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        device = 'simulator'  # the device to run on
        shots = 1  # the number of shots in the experiment.

        apiconnection = QP_program.set_api(API_TOKEN, URL)
        result = QP_program.execute(['qc2'], device, shots, max_credits=3)
        self.assertEqual(result["status"], "COMPLETED")
    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_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_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)
Пример #34
0
    def test_create_add_gates(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("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.get_qasm('circuitName')

        self.assertEqual(len(result), 348)
Пример #35
0
    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)
Пример #36
0
    def test_local_unitary_simulator(self):
        QP_program = QuantumProgram(specs=QPS_SPECS)

        qc = QP_program.get_circuit("circuitName")
        qr = QP_program.get_quantum_registers("qname")
        cr = QP_program.get_classical_registers("cname")

        qc2 = QP_program.create_circuit("qc2", ["qname"], ["cname"])
        qc3 = QP_program.create_circuit("qc3", ["qname"], ["cname"])
        qc2.h(qr[0])
        qc3.h(qr[0])
        qc2.measure(qr[0], cr[0])
        qc3.measure(qr[0], cr[0])
        circuits = ['qc2', 'qc3']

        device = 'local_unitary_simulator'  # the device to run on
        shots = 1  # the number of shots in the experiment.
        credits = 3
        coupling_map = None
        result = QP_program.execute(circuits, device, shots)
        # print(result)
        self.assertEqual(result['status'], 'COMPLETED')
Пример #37
0
    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_load_qasm_text(self):
        """Test load_qasm_text and get_circuit.

        If all is correct we should get the qasm file loaded from the string

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        QASM_string = "// A simple 8 qubit example\nOPENQASM 2.0;\n"
        QASM_string += "include \"qelib1.inc\";\nqreg a[4];\n"
        QASM_string += "qreg b[4];\ncreg c[4];\ncreg d[4];\nh a;\ncx a, b;\n"
        QASM_string += "barrier a;\nbarrier b;\nmeasure a[0]->c[0];\n"
        QASM_string += "measure a[1]->c[1];\nmeasure a[2]->c[2];\n"
        QASM_string += "measure a[3]->c[3];\nmeasure b[0]->d[0];\n"
        QASM_string += "measure b[1]->d[1];\nmeasure b[2]->d[2];\n"
        QASM_string += "measure b[3]->d[3];"
        name = QP_program.load_qasm_text(QASM_string, verbose=False)
        result = QP_program.get_circuit(name)
        to_check = result.qasm()
        # print(to_check)
        self.assertEqual(len(to_check), 554)
    def test_load_qasm_text(self):
        """Test load_qasm_text and get_circuit.

        If all is correct we should get the qasm file loaded from the string

        Previusly:
            Libraries:
                from qiskit import QuantumProgram
        """
        QP_program = QuantumProgram()
        QASM_string = "// A simple 8 qubit example\nOPENQASM 2.0;\n"
        QASM_string += "include \"qelib1.inc\";\nqreg a[4];\n"
        QASM_string += "qreg b[4];\ncreg c[4];\ncreg d[4];\nh a;\ncx a, b;\n"
        QASM_string += "barrier a;\nbarrier b;\nmeasure a[0]->c[0];\n"
        QASM_string += "measure a[1]->c[1];\nmeasure a[2]->c[2];\n"
        QASM_string += "measure a[3]->c[3];\nmeasure b[0]->d[0];\n"
        QASM_string += "measure b[1]->d[1];\nmeasure b[2]->d[2];\n"
        QASM_string += "measure b[3]->d[3];"
        name = QP_program.load_qasm_text(QASM_string, verbose=False)
        result = QP_program.get_circuit(name)
        to_check = result.qasm()
        # print(to_check)
        self.assertEqual(len(to_check), 554)
    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')
Пример #42
0
        "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)
Пример #43
0
Q_SPECS = {
    "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)])
Пример #44
0
 def test_get_circuit_noname(self):
     q_program = QuantumProgram(specs=self.qps_specs_nonames)
     qc = q_program.get_circuit()
     self.assertIsInstance(qc, QuantumCircuit)
Пример #45
0
###############################################################
QPS_SPECS = {
    "circuits": [{
        "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.
Пример #46
0
        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)
qft4.barrier()
for j in range(4):
    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})
Пример #49
0
             "size": n},
            {"name": "b",
             "size": n},
            {"name": "cin",
             "size": 1},
            {"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):
Пример #50
0
 def test_get_circuit_noname(self):
     q_program = QuantumProgram(specs=self.QPS_SPECS_NONAMES)
     qc = q_program.get_circuit()
     self.assertIsInstance(qc, QuantumCircuit)
Пример #51
0
                "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)
Пример #52
0
        }]
    }, {
        "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_registers("q")
c = qp.get_classical_registers("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
Пример #53
0
###############################################################
Q_SPECS = {
    "name": "Program-tutorial",
    "circuits": [{
        "name": "initializer_circ",
        "quantum_registers": [{
            "name": "qr",
            "size": 4
        }],
        "classical_registers": [{
            "name": "cr",
            "size": 4
        }]}],
}
Q_program = QuantumProgram(specs=Q_SPECS)
circuit = Q_program.get_circuit("initializer_circ")
qr = Q_program.get_quantum_register("qr")
cr = Q_program.get_classical_register('cr')

desired_vector = [
    1 / math.sqrt(4) * complex(0, 1),
    1 / math.sqrt(8) * complex(1, 0),
    0,
    0,
    0,
    0,
    0,
    0,
    1 / math.sqrt(8) * complex(1, 0),
    1 / math.sqrt(8) * complex(0, 1),
    0,