Exemplo n.º 1
0
def test_non_clifford_known_matrix():
    q0 = cirq.LineQubit(0)
    circuit = cirq.Circuit.from_ops(cirq.Z(q0)**0.25, )
    c_orig = cirq.Circuit(circuit)

    ConvertToCliffordGates(ignore_failures=True).optimize_circuit(circuit)
    assert circuit == c_orig

    circuit2 = cirq.Circuit(c_orig)
    with pytest.raises(ValueError):
        ConvertToCliffordGates().optimize_circuit(circuit2)
Exemplo n.º 2
0
def test_convert_composite():
    class CompositeDummy(cirq.Gate, cirq.CompositeGate):
        def default_decompose(self, qubits):
            q0, q1 = qubits
            yield cirq.X(q0)
            yield cirq.Y(q1)**0.5
            yield cirq.H(q0)

    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit.from_ops(CompositeDummy()(q0, q1))
    c_orig = cirq.Circuit(circuit)
    ConvertToCliffordGates().optimize_circuit(circuit)

    assert all(
        isinstance(op.gate, cirq.CliffordGate)
        for op in circuit.all_operations())

    cirq.testing.assert_allclose_up_to_global_phase(
        circuit.to_unitary_matrix(), c_orig.to_unitary_matrix(), atol=1e-7)

    cirq.testing.assert_has_diagram(
        circuit, """
0: ───X───────H───

1: ───Y^0.5───────
""")
Exemplo n.º 3
0
def test_already_converted():
    q0 = cirq.LineQubit(0)
    circuit = cirq.Circuit.from_ops(cirq.CliffordGate.H(q0), )
    c_orig = cirq.Circuit(circuit)
    ConvertToCliffordGates().optimize_circuit(circuit)

    assert circuit == c_orig
Exemplo n.º 4
0
def test_convert():
    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit.from_ops(
        cirq.X(q0),
        cirq.Y(q1)**0.5,
        cirq.Z(q0)**-0.5,
        cirq.Z(q1)**0,
        cirq.H(q0),
    )
    c_orig = cirq.Circuit(circuit)
    ConvertToCliffordGates().optimize_circuit(circuit)

    assert all(
        isinstance(op.gate, cirq.CliffordGate)
        for op in circuit.all_operations())

    cirq.testing.assert_allclose_up_to_global_phase(
        circuit.to_unitary_matrix(), c_orig.to_unitary_matrix(), atol=1e-7)

    cirq.testing.assert_has_diagram(
        circuit, """
0: ───X───────Z^-0.5───H───

1: ───Y^0.5───I────────────
""")
Exemplo n.º 5
0
def test_fail_unsupported_gate():
    class UnsupportedDummy(cirq.Gate):
        pass

    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit.from_ops(UnsupportedDummy()(q0, q1), )
    with pytest.raises(TypeError):
        ConvertToCliffordGates().optimize_circuit(circuit)
Exemplo n.º 6
0
def test_ignore_unsupported_gate():
    class UnsupportedDummy(cirq.Gate):
        pass

    q0, q1 = cirq.LineQubit.range(2)
    circuit = cirq.Circuit.from_ops(UnsupportedDummy()(q0, q1), )
    c_orig = cirq.Circuit(circuit)
    ConvertToCliffordGates(ignore_failures=True).optimize_circuit(circuit)

    assert circuit == c_orig
Exemplo n.º 7
0
def test_rotation_to_clifford_gate():
    conv = ConvertToCliffordGates()

    assert (conv._rotation_to_clifford_gate(cirq.Pauli.X,
                                            0.0) == cirq.CliffordGate.I)
    assert (conv._rotation_to_clifford_gate(cirq.Pauli.X,
                                            0.5) == cirq.CliffordGate.X_sqrt)
    assert (conv._rotation_to_clifford_gate(cirq.Pauli.X,
                                            1.0) == cirq.CliffordGate.X)
    assert (conv._rotation_to_clifford_gate(cirq.Pauli.X,
                                            -0.5) == cirq.CliffordGate.X_nsqrt)