Ejemplo n.º 1
0
def _random_double_partial_cz_effect():
    return linalg.dot(
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)),
        np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]),
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)),
        np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]),
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
Ejemplo n.º 2
0
def _random_double_full_cz_effect():
    return linalg.dot(
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)),
        ops.CZ.matrix(),
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)),
        ops.CZ.matrix(),
        linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
def _merge_into_matrix_gate_op(
        qubit: ops.QubitId,
        operations: Iterable[ops.Operation]) -> ops.Operation:
    matrix = linalg.dot(
        np.eye(2, dtype=np.complex128),
        *(reversed([protocols.unitary(op) for op in operations])))
    return ops.SingleQubitMatrixGate(matrix).on(qubit)
Ejemplo n.º 4
0
 def _unitary_(self) -> np.ndarray:
     # Source: https://arxiv.org/abs/1707.03429 (equation 2)
     operations = [
         ops.Rz(self.phi * np.pi),
         ops.Ry(self.theta * np.pi),
         ops.Rz(self.lmda * np.pi),
     ]
     return linalg.dot(*map(protocols.unitary, operations))
Ejemplo n.º 5
0
    def _merge_rotations(
            self, qubit: ops.QubitId,
            operations: Iterable[ops.Operation]) -> List[ops.Operation]:
        matrix = linalg.dot(
            np.eye(2, dtype=np.complex128),
            *reversed([protocols.unitary(op) for op in operations]))

        out_gates = single_qubit_matrix_to_native_gates(matrix, self.tolerance)
        return [gate(qubit) for gate in out_gates]
Ejemplo n.º 6
0
def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate(
        pre_turns, post_turns):
    intended_effect = linalg.dot(
        ops.RotZGate(half_turns=2 * pre_turns).matrix(), ops.X.matrix(),
        ops.RotZGate(half_turns=2 * post_turns).matrix())

    gates = decompositions.single_qubit_matrix_to_native_gates(
        intended_effect, tolerance=0.0001)

    assert len(gates) == 1
    assert_gates_implement_unitary(gates, intended_effect)
Ejemplo n.º 7
0
def recompose_kak(g, a, v, b) -> np.ndarray:
    a1, a0 = a
    x, y, z = v
    b1, b0 = b
    xx = linalg.kron(X, X)
    yy = linalg.kron(Y, Y)
    zz = linalg.kron(Z, Z)

    a = linalg.kron(a1, a0)
    m = linalg.map_eigenvalues(xx * x + yy * y + zz * z,
                               lambda e: np.exp(1j * e))
    b = linalg.kron(b1, b0)

    return linalg.dot(a, m, b) * g
Ejemplo n.º 8
0
    def _rewrite(self, operations: List[ops.Operation]) -> Optional[ops.OP_TREE]:
        if not operations:
            return None
        q = operations[0].qubits[0]

        # Custom rewriter?
        if self._rewriter is not None:
            return self._rewriter(operations)

        unitary = linalg.dot(*(protocols.unitary(op) for op in operations[::-1]))

        # Custom synthesizer?
        if self._synthesizer is not None:
            return self._synthesizer(q, unitary)

        # Just use the default.
        return ops.MatrixGate(unitary).on(q)