def _define(self): """ gate iswap a,b { s q[0]; s q[1]; h q[0]; cx q[0],q[1]; cx q[1],q[0]; h q[1]; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .h import HGate from .s import SGate from .x import CXGate q = QuantumRegister(2, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(SGate(), [q[0]], []), (SGate(), [q[1]], []), (HGate(), [q[0]], []), (CXGate(), [q[0], q[1]], []), (CXGate(), [q[1], q[0]], []), (HGate(), [q[1]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate cu3(theta,phi,lambda) c, t { u1(pi/2) t; cx c,t; u3(-theta/2,0,0) t; cx c,t; u3(theta/2,-pi/2,0) t; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u1 import U1Gate from .u3 import U3Gate from .x import CXGate # q_0: ─────────────■───────────────────■──────────────────── # ┌─────────┐┌─┴─┐┌─────────────┐┌─┴─┐┌────────────────┐ # q_1: ┤ U1(π/2) ├┤ X ├┤ U3(0/2,0,0) ├┤ X ├┤ U3(0/2,-π/2,0) ├ # └─────────┘└───┘└─────────────┘└───┘└────────────────┘ q = QuantumRegister(2, "q") qc = QuantumCircuit(q, name=self.name) rules = [ (U1Gate(pi / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (U3Gate(-self.params[0] / 2, 0, 0), [q[1]], []), (CXGate(), [q[0], q[1]], []), (U3Gate(self.params[0] / 2, -pi / 2, 0), [q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """Calculate a subcircuit that implements this unitary.""" # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate from .rx import RXGate from .rz import RZGate # ┌─────────┐ ┌──────────┐ # q_0: ┤ Rx(π/2) ├──■─────────────■──┤ Rx(-π/2) ├ # ├─────────┤┌─┴─┐┌───────┐┌─┴─┐├──────────┤ # q_1: ┤ Rx(π/2) ├┤ X ├┤ Rz(0) ├┤ X ├┤ Rx(-π/2) ├ # └─────────┘└───┘└───────┘└───┘└──────────┘ q = QuantumRegister(2, "q") theta = self.params[0] qc = QuantumCircuit(q, name=self.name) rules = [ (RXGate(np.pi / 2), [q[0]], []), (RXGate(np.pi / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (RZGate(theta), [q[1]], []), (CXGate(), [q[0], q[1]], []), (RXGate(-np.pi / 2), [q[0]], []), (RXGate(-np.pi / 2), [q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate cu3(theta,phi,lambda) c, t { u1((lambda+phi)/2) c; u1((lambda-phi)/2) t; cx c,t; u3(-theta/2,0,-(phi+lambda)/2) t; cx c,t; u3(theta/2,phi,0) t; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u1 import U1Gate from .x import CXGate # pylint: disable=cyclic-import q = QuantumRegister(2, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(U1Gate((self.params[2] + self.params[1]) / 2), [q[0]], []), (U1Gate((self.params[2] - self.params[1]) / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (U3Gate(-self.params[0] / 2, 0, -(self.params[1] + self.params[2]) / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (U3Gate(self.params[0] / 2, self.params[1], 0), [q[1]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate crz(lambda) a,b { rz(lambda/2) b; cx a,b; rz(-lambda/2) b; cx a,b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate # q_0: ─────────────■────────────────■── # ┌─────────┐┌─┴─┐┌──────────┐┌─┴─┐ # q_1: ┤ Rz(λ/2) ├┤ X ├┤ Rz(-λ/2) ├┤ X ├ # └─────────┘└───┘└──────────┘└───┘ q = QuantumRegister(2, "q") qc = QuantumCircuit(q, name=self.name) rules = [ (RZGate(self.params[0] / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (RZGate(-self.params[0] / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate ccx a,b,c { h c; cx b,c; tdg c; cx a,c; t c; cx b,c; tdg c; cx a,c; t b; t c; h c; cx a,b; t a; tdg b; cx a,b;} """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit q = QuantumRegister(3, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(HGate(), [q[2]], []), (CXGate(), [q[1], q[2]], []), (TdgGate(), [q[2]], []), (CXGate(), [q[0], q[2]], []), (TGate(), [q[2]], []), (CXGate(), [q[1], q[2]], []), (TdgGate(), [q[2]], []), (CXGate(), [q[0], q[2]], []), (TGate(), [q[1]], []), (TGate(), [q[2]], []), (HGate(), [q[2]], []), (CXGate(), [q[0], q[1]], []), (TGate(), [q[0]], []), (TdgGate(), [q[1]], []), (CXGate(), [q[0], q[1]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate rzz(theta) a, b { cx a, b; u1(theta) b; cx a, b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate from .rz import RZGate # q_0: ──■─────────────■── # ┌─┴─┐┌───────┐┌─┴─┐ # q_1: ┤ X ├┤ Rz(0) ├┤ X ├ # └───┘└───────┘└───┘ q = QuantumRegister(2, "q") theta = self.params[0] qc = QuantumCircuit(q, name=self.name) rules = [ (CXGate(), [q[0], q[1]], []), (RZGate(theta), [q[1]], []), (CXGate(), [q[0], q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate rccx a,b,c { u2(0,pi) c; u1(pi/4) c; cx b, c; u1(-pi/4) c; cx a, c; u1(pi/4) c; cx b, c; u1(-pi/4) c; u2(0,pi) c; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit q = QuantumRegister(3, 'q') qc = QuantumCircuit(q, name=self.name) rules = [ (U2Gate(0, pi), [q[2]], []), # H gate (U1Gate(pi / 4), [q[2]], []), # T gate (CXGate(), [q[1], q[2]], []), (U1Gate(-pi / 4), [q[2]], []), # inverse T gate (CXGate(), [q[0], q[2]], []), (U1Gate(pi / 4), [q[2]], []), (CXGate(), [q[1], q[2]], []), (U1Gate(-pi / 4), [q[2]], []), # inverse T gate (U2Gate(0, pi), [q[2]], []), # H gate ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _circuit_rr(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): qr = QuantumRegister(1, 'qr') circuit = QuantumCircuit(qr, global_phase=phase) if not simplify or not math.isclose(theta, -np.pi, abs_tol=atol): circuit._append(RGate(theta + np.pi, np.pi / 2 - lam), [qr[0]], []) circuit._append(RGate(-np.pi, 0.5 * (phi - lam + np.pi)), [qr[0]], []) return circuit
def _define(self): """ gate ch a,b { s b; h b; t b; cx a, b; tdg b; h b; sdg b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate # pylint: disable=cyclic-import q = QuantumRegister(2, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(SGate(), [q[1]], []), (HGate(), [q[1]], []), (TGate(), [q[1]], []), (CXGate(), [q[0], q[1]], []), (TdgGate(), [q[1]], []), (HGate(), [q[1]], []), (SdgGate(), [q[1]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate cu1(lambda) a,b { u1(lambda/2) a; cx a,b; u1(-lambda/2) b; cx a,b; u1(lambda/2) b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate # pylint: disable=cyclic-import q = QuantumRegister(2, "q") qc = QuantumCircuit(q, name=self.name) rules = [ (U1Gate(self.params[0] / 2), [q[0]], []), (CXGate(), [q[0], q[1]], []), (U1Gate(-self.params[0] / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (U1Gate(self.params[0] / 2), [q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate c3sqrtx a,b,c,d { h d; cu1(-pi/8) a,d; h d; cx a,b; h d; cu1(pi/8) b,d; h d; cx a,b; h d; cu1(-pi/8) b,d; h d; cx b,c; h d; cu1(pi/8) c,d; h d; cx a,c; h d; cu1(-pi/8) c,d; h d; cx b,c; h d; cu1(pi/8) c,d; h d; cx a,c; h d; cu1(-pi/8) c,d; h d; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u1 import CU1Gate q = QuantumRegister(4, name="q") # pylint: disable=invalid-unary-operand-type rules = [ (HGate(), [q[3]], []), (CU1Gate(-self._angle), [q[0], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[0], q[1]], []), (HGate(), [q[3]], []), (CU1Gate(self._angle), [q[1], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[0], q[1]], []), (HGate(), [q[3]], []), (CU1Gate(-self._angle), [q[1], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[1], q[2]], []), (HGate(), [q[3]], []), (CU1Gate(self._angle), [q[2], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[0], q[2]], []), (HGate(), [q[3]], []), (CU1Gate(-self._angle), [q[2], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[1], q[2]], []), (HGate(), [q[3]], []), (CU1Gate(self._angle), [q[2], q[3]], []), (HGate(), [q[3]], []), (CXGate(), [q[0], q[2]], []), (HGate(), [q[3]], []), (CU1Gate(-self._angle), [q[2], q[3]], []), (HGate(), [q[3]], []), ] qc = QuantumCircuit(q) for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _circuit_u3(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): qr = QuantumRegister(1, "qr") circuit = QuantumCircuit(qr, global_phase=phase) phi = _mod_2pi(phi, atol) lam = _mod_2pi(lam, atol) if not simplify or abs(theta) > atol or abs(phi) > atol or abs(lam) > atol: circuit._append(U3Gate(theta, phi, lam), [qr[0]], []) return circuit
def _define(self): """The standard definition used the Gray code implementation.""" # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit q = QuantumRegister(self.num_qubits, name='q') qc = QuantumCircuit(q) qc._append(MCXGrayCode(self.num_ctrl_qubits), q[:], []) self.definition = qc
def _define(self): theta = self.params[0] q = QuantumRegister(self.num_qubits, "q") qc = QuantumCircuit(q, name=self.name) for i in range(self.num_qubits): for j in range(i + 1, self.num_qubits): qc._append(RXXGate(theta), [q[i], q[j]], []) self.definition = qc
def _circuit_rr(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 and abs(phi) < atol and abs(lam) < atol: return circuit if abs(theta - np.pi) > atol: circuit._append(RGate(theta - np.pi, _mod_2pi(np.pi / 2 - lam, atol)), [qr[0]], []) circuit._append(RGate(np.pi, _mod_2pi(0.5 * (phi - lam + np.pi), atol)), [qr[0]], []) return circuit
def _define(self): # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u3 import U3Gate q = QuantumRegister(1, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(U3Gate(pi / 2, self.params[0], self.params[1]), [q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _circuit_u(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): # pylint: disable=unused-argument qr = QuantumRegister(1, 'qr') circuit = QuantumCircuit(qr, global_phase=phase) circuit._append(UGate(theta, phi, lam), [qr[0]], []) return circuit
def _define(self): # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .rxx import RXXGate theta = self.params[0] q = QuantumRegister(self.num_qubits, 'q') qc = QuantumCircuit(q, name=self.name) for i in range(self.num_qubits): for j in range(i + 1, self.num_qubits): qc._append(RXXGate(theta), [q[i], q[j]], []) self.definition = qc
def _define(self): """ gate xy(theta, beta) a, b { rz(beta) b; rz(-pi/2) a; sx a; rz(pi/2) a; s b; cx a, b; ry(theta) a; ry(theta) b; cx a, b; sdg b; rz(-pi/2) a; sxdg a; rz(pi/2) a; rz(-beta) b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate from .s import SGate, SdgGate from .sx import SXGate, SXdgGate from .rz import RZGate from .ry import RYGate theta = self.params[0] beta = self.params[1] q = QuantumRegister(2, "q") qc = QuantumCircuit(q, name=self.name) rules = [ (RZGate(beta), [q[1]], []), (RZGate(-pi / 2), [q[0]], []), (SXGate(), [q[0]], []), (RZGate(pi / 2), [q[0]], []), (SGate(), [q[1]], []), (CXGate(), [q[0], q[1]], []), (RYGate(theta / 2), [q[0]], []), (RYGate(theta / 2), [q[1]], []), (CXGate(), [q[0], q[1]], []), (SdgGate(), [q[1]], []), (RZGate(-pi / 2), [q[0]], []), (SXdgGate(), [q[0]], []), (RZGate(pi / 2), [q[0]], []), (RZGate(-beta), [q[1]], []), ] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def dag_to_circuit(dag): """Build a ``QuantumCircuit`` object from a ``DAGCircuit``. Args: dag (DAGCircuit): the input dag. Return: QuantumCircuit: the circuit representing the input dag. Example: .. jupyter-execute:: from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit from qiskit.dagcircuit import DAGCircuit from qiskit.converters import circuit_to_dag from qiskit.circuit.library.standard_gates import CHGate, U2Gate, CXGate from qiskit.converters import dag_to_circuit %matplotlib inline q = QuantumRegister(3, 'q') c = ClassicalRegister(3, 'c') circ = QuantumCircuit(q, c) circ.h(q[0]) circ.cx(q[0], q[1]) circ.measure(q[0], c[0]) circ.rz(0.5, q[1]).c_if(c, 2) dag = circuit_to_dag(circ) circuit = dag_to_circuit(dag) circuit.draw() """ name = dag.name or None circuit = QuantumCircuit( dag.qubits, dag.clbits, *dag.qregs.values(), *dag.cregs.values(), name=name, global_phase=dag.global_phase, ) circuit.metadata = dag.metadata circuit.calibrations = dag.calibrations for node in dag.topological_op_nodes(): # Get arguments for classical control (if any) inst = node.op.copy() circuit._append(inst, node.qargs, node.cargs) circuit.duration = dag.duration circuit.unit = dag.unit return circuit
def _circuit_xyx(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): gphase = phase - (phi + lam) / 2 qr = QuantumRegister(1, "qr") circuit = QuantumCircuit(qr) if not simplify: atol = -1.0 if abs(theta) < atol: tot = _mod_2pi(phi + lam, atol) if abs(tot) > atol: circuit._append(RXGate(tot), [qr[0]], []) gphase += tot / 2 circuit.global_phase = gphase return circuit if abs(theta - np.pi) < atol: gphase += phi lam, phi = lam - phi, 0 lam = _mod_2pi(lam, atol) if abs(lam) > atol: gphase += lam / 2 circuit._append(RXGate(lam), [qr[0]], []) circuit._append(RYGate(theta), [qr[0]], []) phi = _mod_2pi(phi, atol) if abs(phi) > atol: gphase += phi / 2 circuit._append(RXGate(phi), [qr[0]], []) circuit.global_phase = gphase return circuit
def _define(self): """ gate ry(theta) a { r(theta, pi/2) a; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .r import RGate q = QuantumRegister(1, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(RGate(self.params[0], pi / 2), [q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate ecr a, b { rzx(pi/4) a, b; x a; rzx(-pi/4) a, b;} """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit q = QuantumRegister(2, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(RZXGate(np.pi / 4), [q[0], q[1]], []), (XGate(), [q[0]], []), (RZXGate(-np.pi / 4), [q[0], q[1]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate h a { u2(0,pi) a; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u2 import U2Gate q = QuantumRegister(1, 'q') qc = QuantumCircuit(q, name=self.name) rules = [(U2Gate(0, pi), [q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """ gate rz(phi) a { u1(phi) a; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u1 import U1Gate q = QuantumRegister(1, 'q') theta = self.params[0] qc = QuantumCircuit(q, name=self.name, global_phase=-theta / 2) rules = [(U1Gate(theta), [q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def inverse(self): """Return the inverse. Note that the resulting Gate object has an empty ``params`` property. """ inverse_gate = Gate( name=self.name + "_dg", num_qubits=self.num_qubits, params=[]) # removing the params because arrays are deprecated definition = QuantumCircuit(*self.definition.qregs) for inst in reversed(self._definition): definition._append( inst.replace(operation=inst.operation.inverse())) inverse_gate.definition = definition return inverse_gate
def _define(self): """ gate s a { u1(pi/2) a; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .u1 import U1Gate q = QuantumRegister(1, "q") qc = QuantumCircuit(q, name=self.name) rules = [(U1Gate(pi / 2), [q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc
def _define(self): """Define the MCX gate using recursion.""" # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit q = QuantumRegister(self.num_qubits, name='q') qc = QuantumCircuit(q, name=self.name) if self.num_qubits == 4: qc._append(C3XGate(), q[:], []) self.definition = qc elif self.num_qubits == 5: qc._append(C4XGate(), q[:], []) self.definition = qc else: self.definition = qc self.definition._data = self._recurse(q[:-1], q_ancilla=q[-1])
def _define(self): """ gate dcx a, b { cx a, b; cx a, b; } """ # pylint: disable=cyclic-import from qiskit.circuit.quantumcircuit import QuantumCircuit from .x import CXGate q = QuantumRegister(2, "q") qc = QuantumCircuit(q, name=self.name) rules = [(CXGate(), [q[0], q[1]], []), (CXGate(), [q[1], q[0]], [])] for instr, qargs, cargs in rules: qc._append(instr, qargs, cargs) self.definition = qc