Exemplo n.º 1
0
def test_assert_allclose_up_to_global_phase():
    assert_allclose_up_to_global_phase(
        np.array([[1]]),
        np.array([[1j]]),
        atol=0)

    with pytest.raises(AssertionError):
        assert_allclose_up_to_global_phase(
            np.array([[1]]),
            np.array([[2]]),
            atol=0)

    assert_allclose_up_to_global_phase(
        np.array([[1e-8, -1, 1e-8]]),
        np.array([[1e-8, 1, 1e-8]]),
        atol=1e-6)

    with pytest.raises(AssertionError):
        assert_allclose_up_to_global_phase(
            np.array([[1e-4, -1, 1e-4]]),
            np.array([[1e-4, 1, 1e-4]]),
            atol=1e-6)

    assert_allclose_up_to_global_phase(
        np.array([[1, 2], [3, 4]]),
        np.array([[-1, -2], [-3, -4]]),
        atol=0)
Exemplo n.º 2
0
def test_decompose(gate, gate_equiv):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit.from_ops(
                    gate(q0),
                ).to_unitary_matrix()
    mat_check = cirq.Circuit.from_ops(
                    gate_equiv(q0),
                ).to_unitary_matrix()
    assert_allclose_up_to_global_phase(mat, mat_check, rtol=1e-7, atol=1e-7)
def test_decompose(gate):
    q0, q1 = cirq.NamedQubit('q0'), cirq.NamedQubit('q1')
    circuit = cirq.Circuit.from_ops(
                    gate(q0, q1))
    cirq.ExpandComposite().optimize_circuit(circuit)
    decompose_mat = circuit.to_unitary_matrix()
    gate_mat = gate.matrix()
    assert_allclose_up_to_global_phase(decompose_mat, gate_mat,
                                       rtol=1e-7, atol=1e-7)
Exemplo n.º 4
0
def test_single_qubit_gate_after_switching_order(gate, other):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit.from_ops(
                    gate(q0),
                    other(q0),
                ).to_unitary_matrix()
    mat_swap = cirq.Circuit.from_ops(
                    gate.equivalent_gate_before(other)(q0),
                    gate(q0),
                ).to_unitary_matrix()
    assert_allclose_up_to_global_phase(mat, mat_swap, rtol=1e-7, atol=1e-7)
Exemplo n.º 5
0
def test_inverse_matrix(gate):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit.from_ops(gate(q0)).to_unitary_matrix()
    mat_inv = cirq.Circuit.from_ops(gate.inverse()(q0)).to_unitary_matrix()
    assert_allclose_up_to_global_phase(mat, mat_inv.T.conj(),
                                       rtol=1e-7, atol=1e-7)
Exemplo n.º 6
0
def test_decompose(gate, gate_equiv):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit.from_ops(gate(q0), ).to_unitary_matrix()
    mat_check = cirq.Circuit.from_ops(gate_equiv(q0), ).to_unitary_matrix()
    assert_allclose_up_to_global_phase(mat, mat_check, rtol=1e-7, atol=1e-7)
Exemplo n.º 7
0
def test_known_matrix(gate, gate_equiv):
    assert cirq.has_unitary(gate)
    mat = cirq.unitary(gate)
    mat_check = cirq.unitary(gate_equiv)
    assert_allclose_up_to_global_phase(mat, mat_check, rtol=1e-7, atol=1e-7)
Exemplo n.º 8
0
def test_known_matrix(gate, gate_equiv):
    mat = gate.matrix()
    mat_check = gate_equiv.matrix()
    assert_allclose_up_to_global_phase(mat, mat_check, rtol=1e-7, atol=1e-7)
Exemplo n.º 9
0
def test_inverse_matrix(gate):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit.from_ops(gate(q0)).to_unitary_matrix()
    mat_inv = cirq.Circuit.from_ops(gate.inverse()(q0)).to_unitary_matrix()
    assert_allclose_up_to_global_phase(mat, mat_inv.T.conj())
Exemplo n.º 10
0
def test_inverse_matrix(gate):
    q0 = cirq.NamedQubit('q0')
    mat = cirq.Circuit(gate(q0)).unitary()
    mat_inv = cirq.Circuit(cirq.inverse(gate)(q0)).unitary()
    assert_allclose_up_to_global_phase(mat, mat_inv.T.conj(), rtol=1e-7, atol=1e-7)