Exemplo n.º 1
0
def test_deprecated_givens_rotation(angle_rads):
    with capture_logging() as log:
        u = cirq.unitary(cirq.GivensRotation(angle_rads))
    assert len(log) == 1
    assert 'deprecated' in log[0].getMessage()
    assert 'GivensRotation' in log[0].getMessage()
    np.testing.assert_allclose(u, cirq.unitary(cirq.givens(angle_rads)))
Exemplo n.º 2
0
def test_givens_rotation_hamiltonian(angle_rads):
    actual = cirq.unitary(cirq.GivensRotation(angle_rads))
    x = np.array([[0, 1], [1, 0]])
    y = np.array([[0, -1j], [1j, 0]])
    yx = np.kron(y, x)
    xy = np.kron(x, y)
    expected = scipy.linalg.expm(-0.5j * angle_rads * (yx - xy))
    assert np.allclose(actual, expected)
Exemplo n.º 3
0
def test_givens_rotation_unitary(angle_rads):
    actual = cirq.unitary(cirq.GivensRotation(angle_rads))
    c = np.cos(angle_rads)
    s = np.sin(angle_rads)
    # yapf: disable
    expected = np.array([[1, 0, 0, 0],
                         [0, c, -s, 0],
                         [0, s, c, 0],
                         [0, 0, 0, 1]])
    # yapf: enable
    assert np.allclose(actual, expected)
Exemplo n.º 4
0
def test_givens_rotation_equivalent_circuit():
    angle_rads = 3 * np.pi / 7
    t = 2 * angle_rads / np.pi
    gate = cirq.GivensRotation(angle_rads)
    q0, q1 = cirq.LineQubit.range(2)
    equivalent_circuit = cirq.Circuit([
        cirq.T(q0),
        cirq.T(q1)**-1,
        cirq.ISWAP(q0, q1)**t,
        cirq.T(q0)**-1,
        cirq.T(q1),
    ])
    assert np.allclose(cirq.unitary(gate), cirq.unitary(equivalent_circuit))
Exemplo n.º 5
0
def test_givens_rotation():
    """Test if the sqrt_iswap synthesis for a givens rotation is correct"""
    thetas = np.linspace(0, 2 * np.pi, 100)
    qubits = [cirq.NamedQubit('a'), cirq.NamedQubit('b')]
    for theta in thetas:
        program = cirq.Circuit.from_ops(
            cirq.GivensRotation(theta).on(qubits[0], qubits[1]))
        unitary = cirq.unitary(program)
        test_program = program.copy()
        cgoc.ConvertToSqrtIswapGates().optimize_circuit(test_program)
        test_unitary = cirq.unitary(test_program)
        np.testing.assert_allclose(
            4,
            np.abs(np.trace(
                np.conjugate(np.transpose(test_unitary)) @ unitary)))
Exemplo n.º 6
0
def test_deprecated_givens_rotation(angle_rads):
    assert np.all(
        cirq.unitary(cirq.GivensRotation(angle_rads)) == cirq.unitary(
            cirq.givens(angle_rads)))
Exemplo n.º 7
0
def test_givens_rotation_has_consistent_protocols(angle_rads):
    cirq.testing.assert_implements_consistent_protocols(
        cirq.GivensRotation(angle_rads), ignoring_global_phase=False)
Exemplo n.º 8
0
def test_compare_ryxxy_to_cirq_equivalent(rads):
    old_gate = ofc.Ryxxy(rads=rads)
    new_gate = cirq.GivensRotation(angle_rads=rads)
    np.testing.assert_allclose(cirq.unitary(old_gate), cirq.unitary(new_gate))