Example #1
0
def test_axis_angle_canonicalize():
    a = cirq.AxisAngleDecomposition(angle=np.pi * 2.3,
                                    axis=(1, 0, 0),
                                    global_phase=1j).canonicalize()
    assert a.global_phase == -1j
    assert a.axis == (1, 0, 0)
    np.testing.assert_allclose(a.angle, np.pi * 0.3, atol=1e-8)

    a = cirq.AxisAngleDecomposition(angle=np.pi / 2,
                                    axis=(-1, 0, 0),
                                    global_phase=1j).canonicalize()
    assert a.global_phase == 1j
    assert a.axis == (1, 0, 0)
    assert a.angle == -np.pi / 2

    a = cirq.AxisAngleDecomposition(angle=np.pi + 0.01,
                                    axis=(1, 0, 0),
                                    global_phase=1j).canonicalize(atol=0.1)
    assert a.global_phase == 1j
    assert a.axis == (1, 0, 0)
    assert a.angle == np.pi + 0.01

    a = cirq.AxisAngleDecomposition(angle=np.pi + 0.01,
                                    axis=(1, 0, 0),
                                    global_phase=1j).canonicalize(atol=0.001)
    assert a.global_phase == -1j
    assert a.axis == (1, 0, 0)
    assert np.isclose(a.angle, -np.pi + 0.01)
Example #2
0
def test_axis_angle_init():
    a = cirq.AxisAngleDecomposition(angle=1, axis=(0, 1, 0), global_phase=1j)
    assert a.angle == 1
    assert a.axis == (0, 1, 0)
    assert a.global_phase == 1j

    with pytest.raises(ValueError, match='normalize'):
        cirq.AxisAngleDecomposition(angle=1, axis=(0, 0.5, 0), global_phase=1)
Example #3
0
def test_axis_angle_decomposition_eq():
    eq = cirq.testing.EqualsTester()

    eq.make_equality_group(
        lambda: cirq.AxisAngleDecomposition(angle=1, axis=(0.8, 0.6, 0), global_phase=-1)
    )
    eq.add_equality_group(cirq.AxisAngleDecomposition(angle=5, axis=(0.8, 0.6, 0), global_phase=-1))
    eq.add_equality_group(cirq.AxisAngleDecomposition(angle=1, axis=(0.8, 0, 0.6), global_phase=-1))
    eq.add_equality_group(cirq.AxisAngleDecomposition(angle=1, axis=(0.8, 0.6, 0), global_phase=1))
Example #4
0
def test_axis_angle():
    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.ry(1e-10))),
        cirq.AxisAngleDecomposition(angle=0, axis=(1, 0, 0), global_phase=1),
        atol=1e-8,
    )
    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.rx(np.pi))),
        cirq.AxisAngleDecomposition(angle=np.pi, axis=(1, 0, 0), global_phase=1),
        atol=1e-8,
    )
    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.X)),
        cirq.AxisAngleDecomposition(angle=np.pi, axis=(1, 0, 0), global_phase=1j),
        atol=1e-8,
    )
    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.X ** 0.5)),
        cirq.AxisAngleDecomposition(
            angle=np.pi / 2, axis=(1, 0, 0), global_phase=np.exp(1j * np.pi / 4)
        ),
        atol=1e-8,
    )
    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.X ** -0.5)),
        cirq.AxisAngleDecomposition(
            angle=-np.pi / 2, axis=(1, 0, 0), global_phase=np.exp(-1j * np.pi / 4)
        ),
    )

    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.Y)),
        cirq.AxisAngleDecomposition(angle=np.pi, axis=(0, 1, 0), global_phase=1j),
        atol=1e-8,
    )

    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.Z)),
        cirq.AxisAngleDecomposition(angle=np.pi, axis=(0, 0, 1), global_phase=1j),
        atol=1e-8,
    )

    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.H)),
        cirq.AxisAngleDecomposition(
            angle=np.pi, axis=(np.sqrt(0.5), 0, np.sqrt(0.5)), global_phase=1j
        ),
        atol=1e-8,
    )

    assert cirq.approx_eq(
        cirq.axis_angle(cirq.unitary(cirq.H ** 0.5)),
        cirq.AxisAngleDecomposition(
            angle=np.pi / 2,
            axis=(np.sqrt(0.5), 0, np.sqrt(0.5)),
            global_phase=np.exp(1j * np.pi / 4),
        ),
        atol=1e-8,
    )
Example #5
0
def test_axis_angle_canonicalize_approx_equal():
    a1 = cirq.AxisAngleDecomposition(angle=np.pi,
                                     axis=(1, 0, 0),
                                     global_phase=1)
    a2 = cirq.AxisAngleDecomposition(angle=-np.pi,
                                     axis=(1, 0, 0),
                                     global_phase=-1)
    b1 = cirq.AxisAngleDecomposition(angle=np.pi,
                                     axis=(1, 0, 0),
                                     global_phase=-1)
    assert cirq.approx_eq(a1, a2, atol=1e-8)
    assert not cirq.approx_eq(a1, b1, atol=1e-8)
Example #6
0
def test_axis_angle_decomposition_repr():
    cirq.testing.assert_equivalent_repr(
        cirq.AxisAngleDecomposition(angle=1,
                                    axis=(0, 0.6, 0.8),
                                    global_phase=-1))