Ejemplo n.º 1
0
    def run(self, dag):
        """Run the CXDirection pass on `dag`.

        Flips the cx nodes to match the directed coupling map. Modifies the
        input dag.

        Args:
            dag (DAGCircuit): DAG to map.

        Returns:
            DAGCircuit: The rearranged dag for the coupling map

        Raises:
            TranspilerError: If the circuit cannot be mapped just by flipping the
                cx nodes.
        """
        cmap_edges = set(self.coupling_map.get_edges())

        if len(dag.qregs) > 1:
            raise TranspilerError(
                'CXDirection expects a single qreg input DAG,'
                'but input DAG had qregs: {}.'.format(dag.qregs))

        trivial_layout = Layout.generate_trivial_layout(*dag.qregs.values())

        for cnot_node in dag.named_nodes('cx', 'CX'):
            control = cnot_node.qargs[0]
            target = cnot_node.qargs[1]

            physical_q0 = trivial_layout[control]
            physical_q1 = trivial_layout[target]

            if self.coupling_map.distance(physical_q0, physical_q1) != 1:
                raise TranspilerError(
                    'The circuit requires a connection between physical '
                    'qubits %s and %s' % (physical_q0, physical_q1))

            if (physical_q0, physical_q1) not in cmap_edges:
                # A flip needs to be done

                # Create the replacement dag and associated register.
                sub_dag = DAGCircuit()
                sub_qr = QuantumRegister(2)
                sub_dag.add_qreg(sub_qr)

                # Add H gates before
                sub_dag.apply_operation_back(U2Gate(0, pi), [sub_qr[0]], [])
                sub_dag.apply_operation_back(U2Gate(0, pi), [sub_qr[1]], [])

                # Flips the cx
                sub_dag.apply_operation_back(CXGate(), [sub_qr[1], sub_qr[0]],
                                             [])

                # Add H gates after
                sub_dag.apply_operation_back(U2Gate(0, pi), [sub_qr[0]], [])
                sub_dag.apply_operation_back(U2Gate(0, pi), [sub_qr[1]], [])

                dag.substitute_node_with_dag(cnot_node, sub_dag)

        return dag
Ejemplo n.º 2
0
 def _circuit_u321(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL):
     qr = QuantumRegister(1, "qr")
     circuit = QuantumCircuit(qr, global_phase=phase)
     if not simplify:
         atol = -1.0
     if abs(theta) < atol:
         tot = _mod_2pi(phi + lam, atol)
         if abs(tot) > atol:
             circuit._append(U1Gate(tot), [qr[0]], [])
     elif abs(theta - np.pi / 2) < atol:
         circuit._append(U2Gate(_mod_2pi(phi, atol), _mod_2pi(lam, atol)), [qr[0]], [])
     else:
         circuit._append(U3Gate(theta, _mod_2pi(phi, atol), _mod_2pi(lam, atol)), [qr[0]], [])
     return circuit
    def test_optimize_u3_to_u2(self):
        """U3(pi/2, 0, pi/4) ->  U2(0, pi/4). Basis [u1, u2, u3]."""
        qr = QuantumRegister(2, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.append(U3Gate(np.pi / 2, 0, np.pi / 4), [qr[0]])

        expected = QuantumCircuit(qr)
        expected.append(U2Gate(0, np.pi / 4), [qr[0]])

        basis = ['u1', 'u2', 'u3']
        passmanager = PassManager()
        passmanager.append(BasisTranslator(sel, basis))
        passmanager.append(Optimize1qGatesDecomposition(basis))
        result = passmanager.run(circuit)

        self.assertEqual(expected, result)
    def test_optimize_u3_to_u2(self):
        """U3(pi/2, 0, pi/4) ->  U2(0, pi/4). Basis [u1, u2, u3]."""
        qr = QuantumRegister(2, "qr")
        circuit = QuantumCircuit(qr)
        circuit.append(U3Gate(np.pi / 2, 0, np.pi / 4), [qr[0]])

        expected = QuantumCircuit(qr)
        expected.append(U2Gate(0, np.pi / 4), [qr[0]])

        basis = ["u1", "u2", "u3"]
        passmanager = PassManager()
        passmanager.append(BasisTranslator(sel, basis))
        passmanager.append(Optimize1qGatesDecomposition(basis))
        result = passmanager.run(circuit)
        self.assertEqual(expected, result)
        msg = f"expected:\n{expected}\nresult:\n{result}"
        self.assertEqual(expected, result, msg=msg)
Ejemplo n.º 5
0
 def _circuit_u321(theta,
                   phi,
                   lam,
                   phase,
                   simplify=True,
                   atol=DEFAULT_ATOL):
     rtol = 1e-9  # default is 1e-5, too far from atol=1e-12
     circuit = QuantumCircuit(1, global_phase=phase)
     if simplify and (np.isclose(theta, 0.0, atol=atol, rtol=rtol)):
         if not np.isclose(
                 phi + lam, [0.0, 2 * np.pi], atol=atol, rtol=rtol).any():
             circuit.append(U1Gate(_mod2pi(phi + lam)), [0])
     elif simplify and np.isclose(theta, np.pi / 2, atol=atol, rtol=rtol):
         circuit.append(U2Gate(phi, lam), [0])
     else:
         circuit.append(U3Gate(theta, phi, lam), [0])
     return circuit
Ejemplo n.º 6
0
 def _circuit_u321(theta,
                   phi,
                   lam,
                   phase,
                   simplify=True,
                   atol=DEFAULT_ATOL):
     rtol = 1e-9  # default is 1e-5, too far from atol=1e-12
     qr = QuantumRegister(1, 'qr')
     circuit = QuantumCircuit(qr, global_phase=phase)
     if simplify and (math.isclose(theta, 0.0, abs_tol=atol, rel_tol=rtol)):
         phi_lam = phi + lam
         if not (math.isclose(phi_lam, 0.0, abs_tol=atol, rel_tol=rtol) or
                 math.isclose(phi_lam, 2*np.pi, abs_tol=atol, rel_tol=rtol)):
             circuit._append(U1Gate(_mod2pi(phi+lam)), [qr[0]], [])
     elif simplify and math.isclose(theta, np.pi/2, abs_tol=atol, rel_tol=rtol):
         circuit._append(U2Gate(phi, lam), [qr[0]], [])
     else:
         circuit._append(U3Gate(theta, phi, lam), [qr[0]], [])
     return circuit
Ejemplo n.º 7
0
from qiskit.circuit import EquivalenceLibrary, Parameter, QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import U2Gate, U3Gate, HGate, U1Gate, CCXGate, TdgGate, TGate, RZGate, CXGate
from qiskit.qasm import pi

eq_lib = EquivalenceLibrary()

theta = Parameter('theta')
phi = Parameter('phi')
lamda = Parameter('lamda')

u3 = U3Gate(theta, phi, lamda)
u2 = U2Gate(theta, phi)
u1 = U1Gate(theta)
ccx = CCXGate()
        
q = QuantumRegister(1, 'q')
equiv_u3 = QuantumCircuit(q)
equiv_u3.append(RZGate(phi+(pi/2)), [q[0]], [])
equiv_u3.append(HGate(),[q[0]], [] )
equiv_u3.append(RZGate(theta),[q[0]], [])
equiv_u3.append(HGate(), [q[0]], [])
equiv_u3.append(RZGate(lamda-(pi/2)),[q[0]], 0)
eq_lib.add_equivalence(u3, equiv_u3)


q = QuantumRegister(1, 'q')
equiv_u2 = QuantumCircuit(q)
equiv_u2.append(RZGate(theta+(pi/2)), [q[0]], [])
equiv_u2.append(HGate(),[q[0]], [] )
equiv_u2.append(RZGate(pi/2),[q[0]], [])
equiv_u2.append(HGate(), [q[0]], [])