def test_unroll_cy(self):
     """test unroll cy"""
     self.circuit.cy(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, -pi / 2), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, pi / 2), [2])
     self.compare_dags()
 def test_unroll_cz(self):
     """test unroll cz"""
     self.circuit.cz(2, 0)
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [0])
     self.ref_circuit.cx(2, 0)
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [0])
     self.compare_dags()
 def test_unroll_crz(self):
     """test unroll crz"""
     self.circuit.crz(0.5, 1, 2)
     self.ref_circuit.append(U3Gate(0, 0, 0.25), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, -0.25), [2])
     self.ref_circuit.cx(1, 2)
 def test_unroll_cry(self):
     """test unroll cry"""
     self.circuit.cry(0.5, 1, 2)
     self.ref_circuit.append(U3Gate(0.25, 0, 0), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(-0.25, 0, 0), [2])
     self.ref_circuit.cx(1, 2)
     self.compare_dags()
 def test_unroll_cu1(self):
     """test unroll cu1"""
     self.circuit.append(CU1Gate(0.1), [0, 2])
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [0])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, -0.05), [2])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [2])
     self.compare_dags()
 def test_unroll_cu3(self):
     """test unroll cu3"""
     self.circuit.append(CU3Gate(0.2, 0.1, 0.0), [1, 2])
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [1])
     self.ref_circuit.append(U3Gate(0, 0, -0.05), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(-0.1, 0, -0.05), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0.1, 0.1, 0), [2])
     self.compare_dags()
 def test_unroll_cswap(self):
     """test unroll cswap"""
     #                     ┌───┐                                                             »
     # qr_0: ─X─     qr_0: ┤ X ├─────────────────■────────────────────────────────────────■──»
     #        │            └─┬─┘                 │                                        │  »
     # qr_1: ─■─  =  qr_1: ──┼───────────────────┼────────────────────■───────────────────┼──»
     #        │              │  ┌─────────────┐┌─┴─┐┌──────────────┐┌─┴─┐┌─────────────┐┌─┴─┐»
     # qr_2: ─X─     qr_2: ──■──┤ U3(π/2,0,π) ├┤ X ├┤ U3(0,0,-π/4) ├┤ X ├┤ U3(0,0,π/4) ├┤ X ├»
     #                          └─────────────┘└───┘└──────────────┘└───┘└─────────────┘└───┘»
     # «      ┌─────────────┐           ┌───┐     ┌──────────────┐┌───┐┌───┐
     # «qr_0: ┤ U3(0,0,π/4) ├───────────┤ X ├─────┤ U3(0,0,-π/4) ├┤ X ├┤ X ├
     # «      └─────────────┘           └─┬─┘     ├─────────────┬┘└─┬─┘└─┬─┘
     # «qr_1: ──────────────────■─────────■───────┤ U3(0,0,π/4) ├───■────┼──
     # «      ┌──────────────┐┌─┴─┐┌─────────────┐├─────────────┤        │
     # «qr_2: ┤ U3(0,0,-π/4) ├┤ X ├┤ U3(0,0,π/4) ├┤ U3(π/2,0,π) ├────────■──
     # «      └──────────────┘└───┘└─────────────┘└─────────────┘
     self.circuit.cswap(1, 0, 2)
     self.ref_circuit.cx(2, 0)
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [2])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, pi / 4), [2])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, pi / 4), [0])
     self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.cx(1, 0)
     self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [0])
     self.ref_circuit.append(U3Gate(0, 0, pi / 4), [1])
     self.ref_circuit.cx(1, 0)
     self.ref_circuit.append(U3Gate(0, 0, pi / 4), [2])
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [2])
     self.ref_circuit.cx(2, 0)
     self.compare_dags()
    def test_unroll_ccx(self):
        """test unroll ccx"""

        # qr_0: ──■──     qr_0: ──────────────────────────────────────■──────────────────────»
        #         │                                                   │                      »
        # qr_1: ──■──  =  qr_1: ─────────────────■────────────────────┼───────────────────■──»
        #       ┌─┴─┐           ┌─────────────┐┌─┴─┐┌──────────────┐┌─┴─┐┌─────────────┐┌─┴─┐»
        # qr_2: ┤ X ├     qr_2: ┤ U3(π/2,0,π) ├┤ X ├┤ U3(0,0,-π/4) ├┤ X ├┤ U3(0,0,π/4) ├┤ X ├»
        #       └───┘           └─────────────┘└───┘└──────────────┘└───┘└─────────────┘└───┘»
        # «                                          ┌─────────────┐
        # «qr_0: ──────────────────■─────────■───────┤ U3(0,0,π/4) ├───■──
        # «      ┌─────────────┐   │       ┌─┴─┐     ├─────────────┴┐┌─┴─┐
        # «qr_1: ┤ U3(0,0,π/4) ├───┼───────┤ X ├─────┤ U3(0,0,-π/4) ├┤ X ├
        # «      ├─────────────┴┐┌─┴─┐┌────┴───┴────┐├─────────────┬┘└───┘
        # «qr_2: ┤ U3(0,0,-π/4) ├┤ X ├┤ U3(0,0,π/4) ├┤ U3(π/2,0,π) ├──────
        # «      └──────────────┘└───┘└─────────────┘└─────────────┘
        self.circuit.ccx(0, 1, 2)
        self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [2])
        self.ref_circuit.cx(1, 2)
        self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [2])
        self.ref_circuit.cx(0, 2)
        self.ref_circuit.append(U3Gate(0, 0, pi / 4), [2])
        self.ref_circuit.cx(1, 2)
        self.ref_circuit.append(U3Gate(0, 0, pi / 4), [1])
        self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [2])
        self.ref_circuit.cx(0, 2)
        self.ref_circuit.cx(0, 1)
        self.ref_circuit.append(U3Gate(0, 0, pi / 4), [0])
        self.ref_circuit.append(U3Gate(0, 0, -pi / 4), [1])
        self.ref_circuit.cx(0, 1)
        self.ref_circuit.append(U3Gate(0, 0, pi / 4), [2])
        self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [2])
        self.compare_dags()
 def test_unroll_cz(self):
     """test unroll cz"""
     #                     ┌─────────────┐┌───┐┌─────────────┐
     # qr_0: ─■─     qr_0: ┤ U3(π/2,0,π) ├┤ X ├┤ U3(π/2,0,π) ├
     #        │   =        └─────────────┘└─┬─┘└─────────────┘
     # qr_2: ─■─     qr_2: ─────────────────■─────────────────
     self.circuit.cz(2, 0)
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [0])
     self.ref_circuit.cx(2, 0)
     self.ref_circuit.append(U3Gate(pi / 2, 0, pi), [0])
     self.compare_dags()
Esempio n. 10
0
 def test_unroll_cy(self):
     """test unroll cy"""
     # qr_1: ──■──     qr_1: ──────────────────■─────────────────
     #       ┌─┴─┐  =        ┌──────────────┐┌─┴─┐┌─────────────┐
     # qr_2: ┤ Y ├     qr_2: ┤ U3(0,0,-π/2) ├┤ X ├┤ U3(0,0,π/2) ├
     #       └───┘           └──────────────┘└───┘└─────────────┘
     self.circuit.cy(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, -pi / 2), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0, 0, pi / 2), [2])
     self.compare_dags()
Esempio n. 11
0
    def test_unroll_crz(self):
        """test unroll crz"""

        # qr_1: ─────■─────     qr_1: ──────────────────■─────────────────────■──
        #       ┌────┴────┐  =        ┌──────────────┐┌─┴─┐┌───────────────┐┌─┴─┐
        # qr_2: ┤ Rz(0.5) ├     qr_2: ┤ U3(0,0,0.25) ├┤ X ├┤ U3(0,0,-0.25) ├┤ X ├
        #       └─────────┘           └──────────────┘└───┘└───────────────┘└───┘
        self.circuit.crz(0.5, 1, 2)
        self.ref_circuit.append(U3Gate(0, 0, 0.25), [2])
        self.ref_circuit.cx(1, 2)
        self.ref_circuit.append(U3Gate(0, 0, -0.25), [2])
        self.ref_circuit.cx(1, 2)
Esempio n. 12
0
 def test_unroll_cry(self):
     """test unroll cry"""
     # qr_1: ─────■─────     qr_1: ──────────────────■─────────────────────■──
     #       ┌────┴────┐  =        ┌──────────────┐┌─┴─┐┌───────────────┐┌─┴─┐
     # qr_2: ┤ Ry(0.5) ├     qr_2: ┤ U3(0.25,0,0) ├┤ X ├┤ U3(-0.25,0,0) ├┤ X ├
     #       └─────────┘           └──────────────┘└───┘└───────────────┘└───┘
     self.circuit.cry(0.5, 1, 2)
     self.ref_circuit.append(U3Gate(0.25, 0, 0), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(-0.25, 0, 0), [2])
     self.ref_circuit.cx(1, 2)
     self.compare_dags()
    def test_unroller_two(self):
        """Test unrolling gate.repeat(2).
        """
        qr = QuantumRegister(1, 'qr')

        circuit = QuantumCircuit(qr)
        circuit.append(SGate().repeat(2), [qr[0]])
        result = PassManager(Unroller('u3')).run(circuit)

        expected = QuantumCircuit(qr)
        expected.append(U3Gate(0, 0, pi / 2), [qr[0]])
        expected.append(U3Gate(0, 0, pi / 2), [qr[0]])

        self.assertEqual(result, expected)
Esempio n. 14
0
    def test_initialize_reset_should_be_removed(self):
        """The reset in front of initializer should be removed when zero state"""
        qr = QuantumRegister(1, "qr")
        qc = QuantumCircuit(qr)
        qc.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])
        qc.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])

        expected = QuantumCircuit(qr)
        expected.append(U3Gate(1.5708, 0, 0), [qr[0]])
        expected.reset(qr[0])
        expected.append(U3Gate(1.5708, 3.1416, 0), [qr[0]])

        after = transpile(qc, basis_gates=['reset', 'u3'], optimization_level=1)
        self.assertEqual(after, expected)
Esempio n. 15
0
 def test_unroll_cu1(self):
     """test unroll cu1"""
     #                            ┌──────────────┐
     # qr_0: ─■────────     qr_0: ┤ U3(0,0,0.05) ├──■─────────────────────■──────────────────
     #        │U1(0.1)   =        └──────────────┘┌─┴─┐┌───────────────┐┌─┴─┐┌──────────────┐
     # qr_2: ─■────────     qr_2: ────────────────┤ X ├┤ U3(0,0,-0.05) ├┤ X ├┤ U3(0,0,0.05) ├
     #                                            └───┘└───────────────┘└───┘└──────────────┘
     self.circuit.append(CU1Gate(0.1), [0, 2])
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [0])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, -0.05), [2])
     self.ref_circuit.cx(0, 2)
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [2])
     self.compare_dags()
Esempio n. 16
0
def make_hard_thetas_oneq(smallest=1e-18,
                          factor=3.2,
                          steps=22,
                          phi=0.7,
                          lam=0.9):
    """Make 1q gates with theta/2 close to 0, pi/2, pi, 3pi/2"""
    return (
        [U3Gate(smallest * factor**i, phi, lam) for i in range(steps)] +
        [U3Gate(-smallest * factor**i, phi, lam) for i in range(steps)] + [
            U3Gate(np.pi / 2 + smallest * factor**i, phi, lam)
            for i in range(steps)
        ] + [
            U3Gate(np.pi / 2 - smallest * factor**i, phi, lam)
            for i in range(steps)
        ] +
        [U3Gate(np.pi + smallest * factor**i, phi, lam)
         for i in range(steps)] +
        [U3Gate(np.pi - smallest * factor**i, phi, lam)
         for i in range(steps)] + [
             U3Gate(3 * np.pi / 2 + smallest * factor**i, phi, lam)
             for i in range(steps)
         ] + [
             U3Gate(3 * np.pi / 2 - smallest * factor**i, phi, lam)
             for i in range(steps)
         ])
Esempio n. 17
0
 def test_unroll_cu3(self):
     """test unroll cu3"""
     #                                ┌──────────────┐
     # q_1: ────────■────────   q_1: ─┤ U3(0,0,0.05) ├──■────────────────────────■───────────────────
     #      ┌───────┴───────┐ =      ┌┴──────────────┤┌─┴─┐┌──────────────────┐┌─┴─┐┌───────────────┐
     # q_2: ┤ U3(0.2,0.1,0) ├   q_2: ┤ U3(0,0,-0.05) ├┤ X ├┤ U3(-0.1,0,-0.05) ├┤ X ├┤ U3(0.1,0.1,0) ├
     #      └───────────────┘        └───────────────┘└───┘└──────────────────┘└───┘└───────────────┘
     self.circuit.append(CU3Gate(0.2, 0.1, 0.0), [1, 2])
     self.ref_circuit.append(U3Gate(0, 0, 0.05), [1])
     self.ref_circuit.append(U3Gate(0, 0, -0.05), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(-0.1, 0, -0.05), [2])
     self.ref_circuit.cx(1, 2)
     self.ref_circuit.append(U3Gate(0.1, 0.1, 0), [2])
     self.compare_dags()
    def test_teleport(self):
        """Test draw teleport circuit."""
        from qiskit.circuit.library import U3Gate
        filename = self._get_resource_path('test_teleport.tex')
        qr = QuantumRegister(3, 'q')
        cr = ClassicalRegister(3, 'c')
        qc = QuantumCircuit(qr, cr)
        # Prepare an initial state
        qc.append(U3Gate(0.3, 0.2, 0.1), [qr[0]])
        # Prepare a Bell pair
        qc.h(qr[1])
        qc.cx(qr[1], qr[2])
        # Barrier following state preparation
        qc.barrier(qr)
        # Measure in the Bell basis
        qc.cx(qr[0], qr[1])
        qc.h(qr[0])
        qc.measure(qr[0], cr[0])
        qc.measure(qr[1], cr[1])
        # Apply a correction
        qc.z(qr[2]).c_if(cr, 1)
        qc.x(qr[2]).c_if(cr, 2)
        qc.measure(qr[2], cr[2])

        circuit_drawer(qc, filename=filename, output='latex_source')

        self.assertEqualToReference(filename)
Esempio n. 19
0
 def test_unroll_rzz(self):
     """test unroll rzz"""
     self.circuit.rzz(0.6, 1, 0)
     self.ref_circuit.cx(1, 0)
     self.ref_circuit.append(U3Gate(0, 0, 0.6), [0])
     self.ref_circuit.cx(1, 0)
     self.compare_dags()
def U3Dataset(angle_step=9,
              other_steps=10,
              shots=1024,
              *noise_list,
              save_dir=None):
    df = pd.DataFrame()

    # iterate over all U3 gates
    for theta, phi, lam in tqdm(
            itertools.product(np.linspace(0,
                                          2 * np.pi,
                                          angle_step,
                                          endpoint=True),
                              repeat=3)):
        # Generate sample data
        circ = QuantumCircuit(1, 1)
        circ.append(U3Gate(theta, phi, lam), [0])
        circ.measure(0, 0)
        new_circ = qiskit.compiler.transpile(circ,
                                             basis_gates=['u3'],
                                             optimization_level=0)
        print('\n', new_circ)
        for readout_params, depol_param, thermal_params in tqdm(
                get_noise_model_params(other_steps)):
            data_point = get_data_point(theta, phi, lam, readout_params,
                                        depol_param, thermal_params, shots)
            df = df.append(data_point, ignore_index=True)

        # Create QuantumCircuit()

    if save_dir is not None:
        df.to_csv(save_dir)
    return df
    def test_3q_schedule(self):
        """Test a schedule that was recommended by David McKay :D"""

        #                          ┌─────────────────┐
        # q0_0: ─────────■─────────┤ U3(3.14,1.57,0) ├────────────────────────
        #              ┌─┴─┐       └┬───────────────┬┘
        # q0_1: ───────┤ X ├────────┤ U2(3.14,1.57) ├───■─────────────────────
        #       ┌──────┴───┴──────┐ └───────────────┘ ┌─┴─┐┌─────────────────┐
        # q0_2: ┤ U2(0.778,0.122) ├───────────────────┤ X ├┤ U2(0.778,0.122) ├
        #       └─────────────────┘                   └───┘└─────────────────┘
        backend = FakeOpenPulse3Q()
        inst_map = backend.defaults().instruction_schedule_map
        q = QuantumRegister(3)
        c = ClassicalRegister(3)
        qc = QuantumCircuit(q, c)
        qc.cx(q[0], q[1])
        qc.append(U2Gate(0.778, 0.122), [q[2]])
        qc.append(U3Gate(3.14, 1.57, 0), [q[0]])
        qc.append(U2Gate(3.14, 1.57), [q[1]])
        qc.cx(q[1], q[2])
        qc.append(U2Gate(0.778, 0.122), [q[2]])
        sched = schedule(qc, backend)
        expected = Schedule(
            inst_map.get("cx", [0, 1]),
            (22, inst_map.get("u2", [1], 3.14, 1.57)),
            (22, inst_map.get("u2", [2], 0.778, 0.122)),
            (24, inst_map.get("cx", [1, 2])),
            (44, inst_map.get("u3", [0], 3.14, 1.57, 0)),
            (46, inst_map.get("u2", [2], 0.778, 0.122)),
        )
        for actual, expected in zip(sched.instructions, expected.instructions):
            self.assertEqual(actual[0], expected[0])
            self.assertEqual(actual[1], expected[1])
    def test_complex_1_qubit_circuit(self):
        q = QuantumRegister(1)
        circ = QuantumCircuit(q)
        circ.append(U3Gate(1, 1, 1), [q[0]])

        rho, psi = run_circuit_and_tomography(circ, q, self.method)
        F_bell = state_fidelity(psi, rho, validate=False)
        self.assertAlmostEqual(F_bell, 1, places=1)
    def test_layout_2503(self, level):
        """Test that a user-given initial layout is respected,
        even if cnots are not in the coupling map.

        See: https://github.com/Qiskit/qiskit-terra/issues/2503
        """
        # build a circuit which works as-is on the coupling map, using the initial layout
        qr = QuantumRegister(3, "q")
        cr = ClassicalRegister(2)
        ancilla = QuantumRegister(17, "ancilla")

        qc = QuantumCircuit(qr, cr)
        qc.append(U3Gate(0.1, 0.2, 0.3), [qr[0]])
        qc.append(U2Gate(0.4, 0.5), [qr[2]])
        qc.barrier()
        qc.cx(qr[0], qr[2])
        initial_layout = [6, 7, 12]

        final_layout = {
            0: ancilla[0],
            1: ancilla[1],
            2: ancilla[2],
            3: ancilla[3],
            4: ancilla[4],
            5: ancilla[5],
            6: qr[0],
            7: qr[1],
            8: ancilla[6],
            9: ancilla[7],
            10: ancilla[8],
            11: ancilla[9],
            12: qr[2],
            13: ancilla[10],
            14: ancilla[11],
            15: ancilla[12],
            16: ancilla[13],
            17: ancilla[14],
            18: ancilla[15],
            19: ancilla[16],
        }

        backend = FakePoughkeepsie()

        qc_b = transpile(qc,
                         backend,
                         initial_layout=initial_layout,
                         optimization_level=level)

        self.assertEqual(qc_b._layout._p2v, final_layout)

        gate_0, qubits_0, _ = qc_b[0]
        gate_1, qubits_1, _ = qc_b[1]

        output_qr = qc_b.qregs[0]
        self.assertIsInstance(gate_0, U3Gate)
        self.assertEqual(qubits_0[0], output_qr[6])
        self.assertIsInstance(gate_1, U2Gate)
        self.assertEqual(qubits_1[0], output_qr[12])
    def test_loading_all_qelib1_gates(self):
        """Test setting up a circuit with all gates defined in qiskit/qasm/libs/qelib1.inc."""
        from qiskit.circuit.library import U1Gate, U2Gate, U3Gate, CU1Gate, CU3Gate, UGate

        all_gates_qasm = os.path.join(self.qasm_dir, "all_gates.qasm")
        qasm_circuit = QuantumCircuit.from_qasm_file(all_gates_qasm)

        ref_circuit = QuantumCircuit(3, 3)

        # abstract gates (legacy)
        ref_circuit.append(UGate(0.2, 0.1, 0.6), [0])
        ref_circuit.cx(0, 1)
        # the hardware primitives
        ref_circuit.append(U3Gate(0.2, 0.1, 0.6), [0])
        ref_circuit.append(U2Gate(0.1, 0.6), [0])
        ref_circuit.append(U1Gate(0.6), [0])
        ref_circuit.id(0)
        ref_circuit.cx(0, 1)
        # the standard single qubit gates
        ref_circuit.u(0.2, 0.1, 0.6, 0)
        ref_circuit.p(0.6, 0)
        ref_circuit.x(0)
        ref_circuit.y(0)
        ref_circuit.z(0)
        ref_circuit.h(0)
        ref_circuit.s(0)
        ref_circuit.t(0)
        ref_circuit.sdg(0)
        ref_circuit.tdg(0)
        ref_circuit.sx(0)
        ref_circuit.sxdg(0)
        # the standard rotations
        ref_circuit.rx(0.1, 0)
        ref_circuit.ry(0.1, 0)
        ref_circuit.rz(0.1, 0)
        # the barrier
        ref_circuit.barrier()
        # the standard user-defined gates
        ref_circuit.swap(0, 1)
        ref_circuit.cswap(0, 1, 2)
        ref_circuit.cy(0, 1)
        ref_circuit.cz(0, 1)
        ref_circuit.ch(0, 1)
        ref_circuit.csx(0, 1)
        ref_circuit.append(CU1Gate(0.6), [0, 1])
        ref_circuit.append(CU3Gate(0.2, 0.1, 0.6), [0, 1])
        ref_circuit.cp(0.6, 0, 1)
        ref_circuit.cu(0.2, 0.1, 0.6, 0, 0, 1)
        ref_circuit.ccx(0, 1, 2)
        ref_circuit.crx(0.6, 0, 1)
        ref_circuit.cry(0.6, 0, 1)
        ref_circuit.crz(0.6, 0, 1)
        ref_circuit.rxx(0.2, 0, 1)
        ref_circuit.rzz(0.2, 0, 1)
        ref_circuit.measure([0, 1, 2], [0, 1, 2])

        self.assertEqual(qasm_circuit, ref_circuit)
Esempio n. 25
0
def generate_parameterized_circuit():
    """Generate a circuit with parameters and parameter expressions."""
    param_circuit = QuantumCircuit(1)
    theta = Parameter("theta")
    lam = Parameter("λ")
    theta_pi = 3.14159 * theta
    pe = theta_pi / lam
    param_circuit.append(U3Gate(theta, theta_pi, lam), [0])
    param_circuit.append(U1Gate(pe), [0])
    param_circuit.append(U2Gate(theta_pi, lam), [0])
    return param_circuit
    def test_optimize_u3_basis_u3(self):
        """U3(pi/2, pi/3, pi/4) (basis[u3]) ->  U3(pi/2, pi/3, pi/4)"""
        qr = QuantumRegister(1, "qr")
        circuit = QuantumCircuit(qr)
        circuit.append(U3Gate(np.pi / 2, np.pi / 3, np.pi / 4), [qr[0]])

        passmanager = PassManager()
        passmanager.append(Optimize1qGates(["u3"]))
        result = passmanager.run(circuit)

        self.assertEqual(circuit, result)
Esempio n. 27
0
    def test_optimize_u3_with_parameters(self):
        """Test correct behavior for u3 gates."""
        phi = Parameter("φ")
        alpha = Parameter("α")
        qr = QuantumRegister(1, "qr")

        qc = QuantumCircuit(qr)
        qc.ry(2 * phi, qr[0])
        qc.ry(alpha, qr[0])
        qc.ry(0.1, qr[0])
        qc.ry(0.2, qr[0])

        passmanager = PassManager([Unroller(["u3"]), Optimize1qGates()])
        result = passmanager.run(qc)

        expected = QuantumCircuit(qr)
        expected.append(U3Gate(2 * phi, 0, 0), [qr[0]])
        expected.append(U3Gate(alpha, 0, 0), [qr[0]])
        expected.append(U3Gate(0.3, 0, 0), [qr[0]])

        self.assertEqual(expected, result)
    def test_unroll_1q_chain_conditional(self):
        """Test unroll chain of 1-qubit gates interrupted by conditional.
        """
        qr = QuantumRegister(1, 'qr')
        cr = ClassicalRegister(1, 'cr')
        circuit = QuantumCircuit(qr, cr)
        circuit.h(qr)
        circuit.tdg(qr)
        circuit.z(qr)
        circuit.t(qr)
        circuit.ry(0.5, qr)
        circuit.rz(0.3, qr)
        circuit.rx(0.1, qr)
        circuit.measure(qr, cr)
        circuit.x(qr).c_if(cr, 1)
        circuit.y(qr).c_if(cr, 1)
        circuit.z(qr).c_if(cr, 1)
        dag = circuit_to_dag(circuit)
        pass_ = UnrollCustomDefinitions(std_eqlib, ['u1', 'u2', 'u3'])
        dag = pass_.run(dag)

        pass_ = BasisTranslator(std_eqlib, ['u1', 'u2', 'u3'])
        unrolled_dag = pass_.run(dag)

        # Pick up -1 * 0.3 / 2 global phase for one RZ -> U1.
        ref_circuit = QuantumCircuit(qr, cr, global_phase=-0.3 / 2)
        ref_circuit.append(U2Gate(0, pi), [qr[0]])
        ref_circuit.append(U1Gate(-pi / 4), [qr[0]])
        ref_circuit.append(U1Gate(pi), [qr[0]])
        ref_circuit.append(U1Gate(pi / 4), [qr[0]])
        ref_circuit.append(U3Gate(0.5, 0, 0), [qr[0]])
        ref_circuit.append(U1Gate(0.3), [qr[0]])
        ref_circuit.append(U3Gate(0.1, -pi / 2, pi / 2), [qr[0]])
        ref_circuit.measure(qr[0], cr[0])
        ref_circuit.append(U3Gate(pi, 0, pi), [qr[0]]).c_if(cr, 1)
        ref_circuit.append(U3Gate(pi, pi / 2, pi / 2), [qr[0]]).c_if(cr, 1)
        ref_circuit.append(U1Gate(pi), [qr[0]]).c_if(cr, 1)
        ref_dag = circuit_to_dag(ref_circuit)

        self.assertEqual(unrolled_dag, ref_dag)
    def test_u_gates(self):
        """Test U 1, 2, & 3 gates"""
        from qiskit.circuit.library import U1Gate, U2Gate, U3Gate, CU1Gate, CU3Gate
        qr = QuantumRegister(4, 'q')
        circuit = QuantumCircuit(qr)
        circuit.append(U1Gate(3 * pi / 2), [0])
        circuit.append(U2Gate(3 * pi / 2, 2 * pi / 3), [1])
        circuit.append(U3Gate(3 * pi / 2, 4.5, pi / 4), [2])
        circuit.append(CU1Gate(pi / 4), [0, 1])
        circuit.append(U2Gate(pi / 2, 3 * pi / 2).control(1), [2, 3])
        circuit.append(CU3Gate(3 * pi / 2, -3 * pi / 4, -pi / 2), [0, 1])

        self.circuit_drawer(circuit, filename='u_gates.png')
class TestParameterCtrlState(QiskitTestCase):
    """Test gate equality with ctrl_state parameter."""
    @data((RXGate(0.5), CRXGate(0.5)), (RYGate(0.5), CRYGate(0.5)),
          (RZGate(0.5), CRZGate(0.5)), (XGate(), CXGate()),
          (YGate(), CYGate()), (ZGate(), CZGate()),
          (U1Gate(0.5), CU1Gate(0.5)), (SwapGate(), CSwapGate()),
          (HGate(), CHGate()), (U3Gate(0.1, 0.2, 0.3), CU3Gate(0.1, 0.2, 0.3)))
    @unpack
    def test_ctrl_state_one(self, gate, controlled_gate):
        """Test controlled gates with ctrl_state
        See https://github.com/Qiskit/qiskit-terra/pull/4025
        """
        self.assertEqual(gate.control(1, ctrl_state='1'), controlled_gate)