Esempio n. 1
0
    def default_decompose(self, qubits):
        a, b, c = qubits

        # Hacky magic: avoid the non-adjacent edge.
        if hasattr(b, 'is_adjacent'):
            if not b.is_adjacent(a):
                b, c = c, b
            elif not b.is_adjacent(c):
                a, b = b, a

        yield common_gates.T.on_each([a, b, c])  # Phase a, b, and c.
        yield common_gates.CNOT(c, b)
        yield common_gates.T(b)**-1  # Counter-phase b ⊕ c.
        yield common_gates.CNOT(a, b)
        yield common_gates.T(b)  # Phase a ⊕ b ⊕ c.
        yield common_gates.CNOT(c, b)
        yield common_gates.T(b)**-1  # Counter-phase a ⊕ b.
        yield common_gates.CNOT(a, b)

        # If a then toggle b and c.
        yield common_gates.CNOT(b, c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)

        yield common_gates.T(c)**-1  # Counter-phase a ⊕ c.

        # If a then un-toggle b and c.
        yield common_gates.CNOT(b, c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
Esempio n. 2
0
    def test_valid_powers(self):
        gcirq = cirq.Circuit()
        qreg = [cirq.LineQubit(i) for i in range(5)]

        gcirq.append(g_ops.X(qreg[0])**-3.67)
        gcirq.append(g_ops.Y(qreg[0])**7.9)
        gcirq.append(g_ops.Z(qreg[0])**sqrt(5))
        gcirq.append(g_ops.S(qreg[0])**-pi)
        gcirq.append(g_ops.T(qreg[0])**(sqrt(7) - pi))
        gcirq.append(g_ops.SWAP(qreg[0], qreg[1])**-0.5)
        gcirq.append(g_ops.ISWAP(qreg[0], qreg[1])**16.0)

        result = cirq_to_qlm(gcirq)
        for i, op in enumerate(result.ops):
            name, params = extract_syntax(result.gateDic[op.gate],
                                          result.gateDic)
            if i == 0:
                self.assertEqual(params[0], -3.67 * pi)
            elif i == 1:
                self.assertEqual(params[0], 7.9 * pi)
            elif i == 2:
                self.assertEqual(params[0], sqrt(5) * pi)
            elif i == 3:
                self.assertEqual(params[0], -pi * pi / 2)
            elif i == 4:
                self.assertEqual(params[0], (sqrt(7) - pi) * pi / 4)
            else:
                continue
Esempio n. 3
0
    def _decompose_inside_control(self,
                                  target1: raw_types.QubitId,
                                  control: raw_types.QubitId,
                                  target2: raw_types.QubitId
                                  ) -> op_tree.OP_TREE:
        """A decomposition assuming the control separates the targets.

        target1: ─@─X───────T──────@────────@─────────X───@─────X^-0.5─
                  │ │              │        │         │   │
        control: ─X─@─X─────@─T^-1─X─@─T────X─@─X^0.5─@─@─X─@──────────
                      │     │        │        │         │   │
        target2: ─────@─H─T─X─T──────X─T^-1───X─T^-1────X───X─H─S^-1───
        """
        a, b, c = target1, control, target2
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, a)
        yield common_gates.CNOT(c, b)
        yield common_gates.H(c)
        yield common_gates.T(c)
        yield common_gates.CNOT(b, c)
        yield common_gates.T(a)
        yield common_gates.T(b)**-1
        yield common_gates.T(c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield common_gates.T(b)
        yield common_gates.T(c)**-1
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield common_gates.X(b)**0.5
        yield common_gates.T(c)**-1
        yield common_gates.CNOT(b, a)
        yield common_gates.CNOT(b, c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield common_gates.H(c)
        yield common_gates.S(c)**-1
        yield common_gates.X(a)**-0.5
Esempio n. 4
0
    def _decompose_inside_control(
        self, target1: 'cirq.Qid', control: 'cirq.Qid', target2: 'cirq.Qid'
    ) -> 'cirq.OP_TREE':
        """A decomposition assuming the control separates the targets.

        target1: ─@─X───────T──────@────────@─────────X───@─────X^-0.5─
                  │ │              │        │         │   │
        control: ─X─@─X─────@─T^-1─X─@─T────X─@─X^0.5─@─@─X─@──────────
                      │     │        │        │         │   │
        target2: ─────@─H─T─X─T──────X─T^-1───X─T^-1────X───X─H─S^-1───
        """
        a, b, c = target1, control, target2
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, a)
        yield common_gates.CNOT(c, b)
        yield common_gates.H(c)
        yield common_gates.T(c)
        yield common_gates.CNOT(b, c)
        yield common_gates.T(a)
        yield common_gates.T(b) ** -1
        yield common_gates.T(c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield common_gates.T(b)
        yield common_gates.T(c) ** -1
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield pauli_gates.X(b) ** 0.5
        yield common_gates.T(c) ** -1
        yield common_gates.CNOT(b, a)
        yield common_gates.CNOT(b, c)
        yield common_gates.CNOT(a, b)
        yield common_gates.CNOT(b, c)
        yield common_gates.H(c)
        yield common_gates.S(c) ** -1
        yield pauli_gates.X(a) ** -0.5