예제 #1
0
def test_single_q_const_depth():
    su = random_special_unitary(2)
    ops = single_qubit_matrix_to_phased_x_z_const_depth(su)
    assert len(ops) == 2
    circuit = cirq.Circuit([op.on(cirq.LineQubit(1)) for op in ops])
    u2 = circuit.unitary()
    cirq.testing.assert_allclose_up_to_global_phase(su, u2, atol=1e-8)
예제 #2
0
    (X, 1j * np.eye(2)),
    (-X, 1j * np.eye(2)),
    (X, X),
] + [(testing.random_unitary(2), testing.random_unitary(2))
     for _ in range(10)])
def test_kron_factor(f1, f2):
    p = linalg.kron(f1, f2)
    g, g1, g2 = linalg.kron_factor_4x4_to_2x2s(p)
    assert abs(np.linalg.det(g1) - 1) < 0.00001
    assert abs(np.linalg.det(g2) - 1) < 0.00001
    assert np.allclose(g * linalg.kron(g1, g2), p)


@pytest.mark.parametrize(
    'f1,f2',
    [(testing.random_special_unitary(2), testing.random_special_unitary(2))
     for _ in range(10)])
def test_kron_factor_special_unitaries(f1, f2):
    p = linalg.kron(f1, f2)
    g, g1, g2 = linalg.kron_factor_4x4_to_2x2s(p)
    assert np.allclose(linalg.kron(g1, g2), p)
    assert abs(g - 1) < 0.000001
    assert linalg.is_special_unitary(g1)
    assert linalg.is_special_unitary(g2)


def test_kron_factor_fail():
    with pytest.raises(ValueError):
        _ = linalg.kron_factor_4x4_to_2x2s(
            linalg.kron_with_controls(linalg.CONTROL_TAG, X))