Beispiel #1
0
 def _get_rule(self, node):
     q = QuantumRegister(node.op.num_qubits, "q")
     if node.name == "u1":
         rule = [(RZGate(node.op.params[0]), [q[0]], [])]
     elif node.name == "u2":
         rule = [
             (RZGate(node.op.params[1]), [q[0]], []),
             (SYGate(), [q[0]], []),
             (RZGate(node.op.params[0]), [q[0]], []),
         ]
     elif node.name == "u3":
         rule = [
             (RZGate(node.op.params[2]), [q[0]], []),
             (RYGate(node.op.params[0]), [q[0]], []),
             (RZGate(node.op.params[1]), [q[0]], []),
         ]
     elif node.name == "cx":
         # // controlled-NOT as per Maslov (2017); this implementation takes s = v = +1
         # gate cx a,b
         # {
         # ry(pi/2) a;
         # ms(pi/2, 0) a,b;
         # rx(-pi/2) a;
         # rx(-pi/2) b;
         # ry(-pi/2) a;
         # }
         rule = [
             (SYGate(), [q[0]], []),
             (MSGate(pi / 2, 0), [q[0], q[1]], []),
             (RXGate(-pi / 2), [q[0]], []),
             (RXGate(-pi / 2), [q[1]], []),
             (RYGate(-pi / 2), [q[0]], []),
         ]
     elif node.name == "rx":
         if node.op.params[0] == pi:
             rule = [(XGate(), [q[0]], [])]
         elif node.op.params[0] == pi / 2:
             rule = [(SXGate(), [q[0]], [])]
         else:
             rule = [(RGate(0, node.op.params[0]), [q[0]], [])]
     elif node.name == "h":
         rule = [
             (ZGate(), [q[0]], []),
             (SYGate(), [q[0]], []),
         ]
     elif node.name == "ry":
         if node.op.params[0] == pi:
             rule = [(YGate(), [q[0]], [])]
         elif node.op.params[0] == pi / 2:
             rule = [(SYGate(), [q[0]], [])]
         else:
             rule = [(RGate(pi / 2, node.op.params[0]), [q[0]], [])]
     else:
         rule = node.op.definition
     return rule
Beispiel #2
0
    def _define(self):
        """
		Gate Y to Rz(-pi/2)·Rx(pi)·Rz(pi/2)
		"""
        definition = []
        q = QuantumRegister(1, 'q')
        rule = [(RZGate(-pi / 2), [q[0]], []), (RXGate(pi), [q[0]], []),
                (RZGate(pi / 2), [q[0]], [])]
        for inst in rule:
            definition.append(inst)
        self.definition = definition
Beispiel #3
0
    def _define(self):
        """
		Gate Ry(theta) to Rz(-pi/2)·Rx(theta)·Rz(pi/2)
		"""
        definition = []
        q = QuantumRegister(1, 'q')
        if self.params[0] == 0:
            rule = [(RZGate(0), [q[0]], [])]
        elif self.params[0] % (2 * pi) == 0:
            rule = [(RZGate(0), [q[0]], [])]
        else:
            rule = [(RZGate(-pi / 2), [q[0]], []),
                    (RXGate(self.params[0]), [q[0]], []),
                    (RZGate(pi / 2), [q[0]], [])]
        for inst in rule:
            definition.append(inst)
        self.definition = definition
Beispiel #4
0
    def _zyz_rule(self):
        """Get the circuit rule for the ZYZ decomposition."""
        q = QuantumRegister(self.num_qubits)
        rule = []

        diag = [1., 1.]
        alpha, beta, gamma, _ = self._zyz_dec()

        if abs(alpha) > _EPS:
            rule += [(RZGate(alpha), [q[0]], [])]
        if abs(beta) > _EPS:
            rule += [(RYGate(beta), [q[0]], [])]
        if abs(gamma) > _EPS:
            if self.up_to_diagonal:
                diag = [np.exp(-1j * gamma / 2.), np.exp(1j * gamma / 2.)]
            else:
                rule += [(RZGate(gamma), [q[0]], [])]

        return rule, diag
Beispiel #5
0
    def _define(self):
        """
		Gate Rz(phi) to Rz(phi)
		"""
        definition = []
        q = QuantumRegister(1, 'q')
        rule = [(RZGate(self.params[0]), [q[0]], [])]
        for inst in rule:
            definition.append(inst)
        self.definition = definition
Beispiel #6
0
 def _define(self):
     """
     gate r(theta, phi) a
     {
     rz(-theta) a;
     rx(phi) a;
     rz(theta) a;
     }
     """
     definition = []
     q = QuantumRegister(1, "q")
     theta, phi = tuple(self.params)
     rule = [
         (RZGate(-theta), [q[0]], []),
         (RXGate(phi), [q[0]], []),
         (RZGate(theta), [q[0]], []),
     ]
     for inst in rule:
         definition.append(inst)
     self.definition = definition
 def test_gate_multiplicity_binding(self):
     """Test binding when circuit contains multiple references to same gate"""
     from qiskit.circuit.library.standard_gates.rz import RZGate
     qc = QuantumCircuit(1)
     theta = Parameter('theta')
     gate = RZGate(theta)
     qc.append(gate, [0], [])
     qc.append(gate, [0], [])
     # test for both `bind_parameters` and `assign_parameters`
     for assign_fun in ['bind_parameters', 'assign_parameters']:
         with self.subTest(assign_fun=assign_fun):
             qc2 = getattr(qc, assign_fun)({theta: 1.0})
             self.assertEqual(len(qc2._parameter_table), 0)
             for gate, _, _ in qc2.data:
                 self.assertEqual(float(gate.params[0]), 1.0)
    def _define(self):
        """
        gate xx_minus_yy(theta, beta) a, b {
            rz(-beta) b;
            rz(-pi/2) a;
            sx a;
            rz(pi/2) a;
            s b;
            cx a, b;
            ry(theta/2) a;
            ry(-theta/2) b;
            cx a, b;
            sdg b;
            rz(-pi/2) a;
            sxdg a;
            rz(pi/2) a;
            rz(beta) b;
        }
        """
        theta, beta = self.params
        register = QuantumRegister(2, "q")
        circuit = QuantumCircuit(register, name=self.name)
        a, b = register
        rules = [
            (RZGate(-beta), [b], []),
            (RZGate(-pi / 2), [a], []),
            (SXGate(), [a], []),
            (RZGate(pi / 2), [a], []),
            (SGate(), [b], []),
            (CXGate(), [a, b], []),
            (RYGate(theta / 2), [a], []),
            (RYGate(-theta / 2), [b], []),
            (CXGate(), [a, b], []),
            (SdgGate(), [b], []),
            (RZGate(-pi / 2), [a], []),
            (SXdgGate(), [a], []),
            (RZGate(pi / 2), [a], []),
            (RZGate(beta), [b], []),
        ]
        for instr, qargs, cargs in rules:
            circuit._append(instr, qargs, cargs)

        self.definition = circuit
Beispiel #9
0
def decompose_gate(gate):
    """
    decompose gate in the list_gate to composition of Rx, Rz, Cz
    input: gate (string type)
    output: quantum gate
    """
    if gate == 'id':
        return (RXGate(0), )
    if gate == 'h':
        return (RZGate(pi / 2), RXGate(pi / 2), RZGate(pi / 2))
    if gate == 'x':
        return (RXGate(pi), )
    if gate == 'y':
        return (RZGate(pi), RXGate(pi))
    if gate == 'z':
        return (RZGate(pi), )
    if gate == 'cx':
        return RZGate(pi / 2), RXGate(pi / 2), RZGate(
            pi / 2), CZGate(), RZGate(pi / 2), RXGate(pi / 2), RZGate(pi / 2)
    if gate == 'cz':
        return (CZGate(), )
    if 'rx' in gate:
        start = gate.find('(')  # find the angle
        end = gate.find(')')
        if 'pi' in gate:
            angle = convert_angle_to_float(gate[start + 1:end])
        else:
            angle = float(gate[start + 1:end])
        return (RXGate(angle), )
    if 'ry' in gate:
        start = gate.find('(')  # find the angle
        end = gate.find(')')
        if 'pi' in gate:
            angle = convert_angle_to_float(gate[start + 1:end])
        else:
            angle = float(gate[start + 1:end])
        return RZGate(-pi / 2), RXGate(angle), RZGate(pi / 2)
    if 'rz' in gate:
        start = gate.find('(')  # find the angle
        end = gate.find(')')
        if 'pi' in gate:
            angle = convert_angle_to_float(gate[start + 1:end])
        else:
            angle = float(gate[start + 1:end])
        return (RZGate(angle), )
    if 'measure' in gate:
        return (Measure(), )