def test_submit(self):
        import qiskit.extensions.standard as standard
        from qiskit.circuit.measure import measure
        from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister

        q = QuantumRegister(2, "q")
        r = QuantumRegister(1, "r")
        c = ClassicalRegister(2, "c")
        ca = ClassicalRegister(1, "c^a")
        qc = QuantumCircuit(q, r, c, ca, name="TestCircuit")
        standard.h(qc, q[0])
        standard.cx(qc, q[0], q[1])
        standard.x(qc, r)
        measure(qc, q, c)
        measure(qc, r, ca)

        provider = AcQuantumProvider()
        provider.enable_account()
        backend = provider.get_backend("SIMULATE")  # type: AcQuantumBackend

        qobj = qiskit.compile(qc, backend=backend)
        job = backend.run(qobj)  # type: AcQuantumJob
        result = job.result()  # type: Result

        self.assertIsNotNone(result)

        counts = result.get_counts()

        self.assertListEqual(list(sorted(counts.keys())), ['1 01', '1 10'])
        self.assertAlmostEqual(counts['1 01'], 512, delta=50)
        self.assertAlmostEqual(counts['1 10'], 512, delta=50)

        job._api.delete_experiment(job.job_id())
Esempio n. 2
0
 def apply(self, qregs, param, circuit):
     # type: (List[Tuple[QuantumRegister, int]], List, QuantumCircuit) -> None
     if len(param) == 0:
         raise Exception('Parameters are missing')
     for i, p in enumerate(param[0]):
         if p == 1:
             x(circuit, qregs[i])
Esempio n. 3
0
    def ccx(self, qc, conditial_case, control_qubits, tgt):
        # type:(CCXToffoli, QuantumCircuit, int, List[Tuple[Register, int]], Union[Tuple[Register, int], QuantumRegister]) -> QuantumCircuit
        """
        Using the Toffoli gate cascade on auxiliary qubits, one can create multi-controlled NOT gate. The routine
        needs to add an auxiliary register called ``
        ccx_ancilla`` which also will be re-used if multiple calls
        of this routine are done within the same circuit. As the cascade is always 'undone', the ancilla register
        is left to the ground state.

        :param qc: the circuit to apply this operation to
        :param conditial_case: an integer whose binary representation signifies the branch to controll on
        :param control_qubits: the qubits that hold the desired conditional case
        :param tgt: the target to be applied the controlled X gate on
        :return: the circuit after application of the gate
        """

        # Prepare conditional case
        bit_string = "{:b}".format(conditial_case).zfill(len(control_qubits))
        for i, b in enumerate(reversed(bit_string)):
            if b == '0':
                x(qc, control_qubits[i])
        standard.barrier(qc)

        ccx_ancilla = None  # type: QuantumRegister
        if len(control_qubits) == 1: # This is just the normal CNOT
            cx(qc, control_qubits[0], tgt)
        elif len(control_qubits) == 2: # This is the simple Toffoli
            ccx(qc, control_qubits[0], control_qubits[1], tgt)
        else:
            # Create ancilla qubit or take the one that is already there
            if 'ccx_ancilla' not in [q.name for q in qc.qregs]:
                ccx_ancilla = QuantumRegister(len(control_qubits) - 1, 'ccx_ancilla')
                qc.add_register(ccx_ancilla)
            else:
                ccx_ancilla = [q for q in qc.qregs if q.name == 'ccx_ancilla'][0]

            # Algorithm
            ccx(qc, control_qubits[0], control_qubits[1], ccx_ancilla[0])
            for i in range(1, ccx_ancilla.size):
                ccx(qc, control_qubits[i], ccx_ancilla[i - 1], ccx_ancilla[i])

            ccx(qc, control_qubits[-1], ccx_ancilla[ccx_ancilla.size - 1], tgt)

            for i in reversed(range(1, ccx_ancilla.size)):
                ccx(qc, control_qubits[i], ccx_ancilla[i - 1], ccx_ancilla[i])
            ccx(qc, control_qubits[0], control_qubits[1], ccx_ancilla[0])

        standard.barrier(qc)
        # Undo the conditional case
        for i, b in enumerate(reversed(bit_string)):
            if b == '0':
                x(qc, control_qubits[i])

        return qc
    def test__gates_from_qobj(self):
        import qiskit.extensions.standard as standard
        from qiskit.circuit.measure import measure
        from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister

        q = QuantumRegister(2, "q")
        r = QuantumRegister(1, "r")
        c = ClassicalRegister(2, "c")
        ca = ClassicalRegister(1, "c^a")
        qc = QuantumCircuit(q, c, r, ca, name="TestCircuit")
        standard.h(qc, q[0])
        standard.cx(qc, q[0], q[1])
        standard.x(qc, r)
        measure(qc, q, c)
        measure(qc, r, ca)

        provider = AcQuantumProvider()
        provider.enable_account()
        backend = provider.get_backend("SIMULATE")

        qobj = qiskit.compile(qc, backend=backend)

        gates = AcQuantumJob._gates_from_qobj(qobj)
        self.assertEqual(len(gates), 8)