コード例 #1
0
ファイル: decompositions_test.py プロジェクト: yy8yy/Cirq
def test_single_qubit_matrix_to_native_gates_tolerance_z():
    z = np.diag([1, np.exp(1j * 0.01)])

    optimized_away = cirq.single_qubit_matrix_to_phased_x_z(z, atol=0.1)
    assert len(optimized_away) == 0

    kept = cirq.single_qubit_matrix_to_phased_x_z(z, atol=0.0001)
    assert len(kept) == 1
コード例 #2
0
ファイル: decompositions_test.py プロジェクト: yy8yy/Cirq
def test_single_qubit_matrix_to_native_gates_tolerance_xy():
    c, s = np.cos(0.01), np.sin(0.01)
    xy = np.array([[c, -s], [s, c]])

    optimized_away = cirq.single_qubit_matrix_to_phased_x_z(xy, atol=0.1)
    assert len(optimized_away) == 0

    kept = cirq.single_qubit_matrix_to_phased_x_z(xy, atol=0.0001)
    assert len(kept) == 1
コード例 #3
0
ファイル: decompositions_test.py プロジェクト: yy8yy/Cirq
def test_single_qubit_matrix_to_native_gates_tolerance_half_turn_phasing():
    a = np.pi / 2 + 0.01
    c, s = np.cos(a), np.sin(a)
    nearly_x = np.array([[c, -s], [s, c]])
    z1 = np.diag([1, np.exp(1j * 1.2)])
    z2 = np.diag([1, np.exp(1j * 1.6)])
    phased_nearly_x = z1.dot(nearly_x).dot(z2)

    optimized_away = cirq.single_qubit_matrix_to_phased_x_z(phased_nearly_x,
                                                            atol=0.1)
    assert len(optimized_away) == 1

    kept = cirq.single_qubit_matrix_to_phased_x_z(phased_nearly_x, atol=0.0001)
    assert len(kept) == 2
コード例 #4
0
    def decompose_to_target_gateset(self, op: 'cirq.Operation',
                                    moment_idx: int) -> DecomposeResult:
        """Method to rewrite the given operation using gates from this gateset.

        Args:
            op: `cirq.Operation` to be rewritten using gates from this gateset.
            moment_idx: Moment index where the given operation `op` occurs in a circuit.

        Returns:
            - An equivalent `cirq.OP_TREE` implementing `op` using gates from this gateset.
            - `None` or `NotImplemented` if does not know how to decompose `op`.
        """
        # Known matrix?
        mat = cirq.unitary(op, None) if len(op.qubits) <= 2 else None
        if mat is not None and len(op.qubits) == 1:
            gates = cirq.single_qubit_matrix_to_phased_x_z(mat)
            return [g.on(op.qubits[0]) for g in gates]
        if mat is not None and len(op.qubits) == 2:
            return cirq.two_qubit_matrix_to_cz_operations(
                op.qubits[0],
                op.qubits[1],
                mat,
                allow_partial_czs=False,
                clean_operations=True)

        return NotImplemented
コード例 #5
0
ファイル: decompositions_test.py プロジェクト: yijingS/Cirq
def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate(
        pre_turns, post_turns):
    intended_effect = cirq.dot(cirq.unitary(cirq.Z**(2 * pre_turns)),
                               cirq.unitary(cirq.X),
                               cirq.unitary(cirq.Z**(2 * post_turns)))

    gates = cirq.single_qubit_matrix_to_phased_x_z(intended_effect,
                                                   atol=0.0001)

    assert len(gates) == 1
    assert_gates_implement_unitary(gates, intended_effect, atol=1e-8)
コード例 #6
0
ファイル: convert_to_xmon_gates.py プロジェクト: daxfohl/Cirq
    def _convert_one(self, op: cirq.Operation) -> cirq.OP_TREE:
        # Known matrix?
        mat = cirq.unitary(op, None) if len(op.qubits) <= 2 else None
        if mat is not None and len(op.qubits) == 1:
            gates = cirq.single_qubit_matrix_to_phased_x_z(mat)
            return [g.on(op.qubits[0]) for g in gates]
        if mat is not None and len(op.qubits) == 2:
            return cirq.two_qubit_matrix_to_cz_operations(
                op.qubits[0], op.qubits[1], mat, allow_partial_czs=True, clean_operations=False
            )

        return NotImplemented
コード例 #7
0
ファイル: decompositions_test.py プロジェクト: yy8yy/Cirq
def test_single_qubit_matrix_to_native_gates_known():
    actual = cirq.single_qubit_matrix_to_phased_x_z(np.array([[0, 1], [1, 0]]),
                                                    atol=0.01)
    assert cirq.approx_eq(actual, [cirq.PhasedXPowGate(phase_exponent=1.0)],
                          atol=1e-9)

    actual = cirq.single_qubit_matrix_to_phased_x_z(np.array([[0, -1j],
                                                              [1j, 0]]),
                                                    atol=0.01)
    assert cirq.approx_eq(actual, [cirq.Y], atol=1e-9)

    actual = cirq.single_qubit_matrix_to_phased_x_z(np.array([[1, 0], [0,
                                                                       -1]]),
                                                    atol=0.01)
    assert cirq.approx_eq(actual, [cirq.Z], atol=1e-9)

    actual = cirq.single_qubit_matrix_to_phased_x_z(np.array([[1, 0], [0,
                                                                       1j]]),
                                                    atol=0.01)
    assert cirq.approx_eq(actual, [cirq.Z**0.5], atol=1e-9)

    actual = cirq.single_qubit_matrix_to_phased_x_z(np.array([[1, 0], [0,
                                                                       -1j]]),
                                                    atol=0.01)
    assert cirq.approx_eq(actual, [cirq.Z**-0.5], atol=1e-9)

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[1, 1], [1, -1]]) * np.sqrt(0.5), atol=0.001)
    assert cirq.approx_eq(
        actual,
        [cirq.PhasedXPowGate(phase_exponent=-0.5, exponent=0.5), cirq.Z**-1],
        atol=1e-9)
コード例 #8
0
def test_single_qubit_matrix_to_native_gates_known():
    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[0, 1], [1, 0]]), atol=0.01)
    assert actual == [cirq.X]

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[0, -1j], [1j, 0]]), atol=0.01)
    assert actual == [cirq.Y]

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[1, 0], [0, -1]]), atol=0.01)
    assert actual == [cirq.Z]

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[1, 0], [0, 1j]]), atol=0.01)
    assert actual == [cirq.Z**0.5]

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[1, 0], [0, -1j]]), atol=0.01)
    assert actual == [cirq.Z**-0.5]

    actual = cirq.single_qubit_matrix_to_phased_x_z(
        np.array([[1, 1], [1, -1]]) * np.sqrt(0.5), atol=0.001)
    assert actual == [cirq.Y**-0.5, cirq.Z]
コード例 #9
0
ファイル: decompositions_test.py プロジェクト: yy8yy/Cirq
def test_single_qubit_matrix_to_native_gates_cases(intended_effect):
    gates = cirq.single_qubit_matrix_to_phased_x_z(intended_effect, atol=1e-6)
    assert len(gates) <= 2
    assert_gates_implement_unitary(gates, intended_effect, atol=1e-5)