Ejemplo n.º 1
0
def test_mcry_gray_n(theta, n):
    u = circuit_to_unitary(Circuit().ry(theta)[0])
    expected = np.eye(2**(n + 1), dtype=complex)
    expected[2**n - 1, 2**n - 1] = u[0, 0]
    expected[2**(n + 1) - 1, 2**n - 1] = u[1, 0]
    expected[2**n - 1, 2**(n + 1) - 1] = u[0, 1]
    expected[2**(n + 1) - 1, 2**(n + 1) - 1] = u[1, 1]
    assert np.allclose(
        circuit_to_unitary(Circuit().mcry_gray(theta, list(range(n)), n)),
        expected)
Ejemplo n.º 2
0
def check_decomposed(g: OneQubitGate, d: Decomposer, ignore_global: bool):
    c1 = Circuit(1, [g])
    c2 = Circuit(1, d(g))
    u1 = circuit_to_unitary(c1)
    u2 = circuit_to_unitary(c2)
    if ignore_global:
        gphase1 = cmath.phase(np.linalg.det(u1))
        gphase2 = cmath.phase(np.linalg.det(u2))
        su1 = u1 * cmath.exp(-0.5j * gphase1)
        su2 = u2 * cmath.exp(-0.5j * gphase2)
        assert np.isclose(np.linalg.det(su1), 1.0)
        assert np.isclose(np.linalg.det(su2), 1.0)
    else:
        su1 = su2 = np.eye(2) # To avoid static analyzer warning.
    try:
        if ignore_global:
            assert np.allclose(su1, su2) or np.allclose(su1, -su2)
        else:
            assert np.allclose(u1, u2)
    except AssertionError:
        print("Orig:", c1)
        print(u1)
        if ignore_global:
            print("-->")
            print(su1)
        print("Conv:", c2)
        print(u2)
        if ignore_global:
            print("-->")
            print(su2)
        if ignore_global:
            print("abs(Orig - Conv):")
            print(np.abs(su1 - su2))
            print("abs(Orig + Conv):")
            print(np.abs(su1 + su2))
        else:
            print("abs(Orig - Conv):")
            print(np.abs(u1 - u2))
        raise
Ejemplo n.º 3
0
def test_mcx_gray_1():
    assert np.allclose(circuit_to_unitary(Circuit().mcx_gray([0], 1)),
                       circuit_to_unitary(Circuit().cx[0, 1]))
Ejemplo n.º 4
0
def test_mcx_gray_2():
    assert np.allclose(circuit_to_unitary(Circuit().mcx_gray([0, 1], 2)),
                       circuit_to_unitary(Circuit().ccx[0, 1, 2]))
Ejemplo n.º 5
0
def test_c4x():
    expected = np.eye(32)
    expected[15, 15] = expected[31, 31] = 0
    expected[15, 31] = expected[31, 15] = 1
    assert np.allclose(circuit_to_unitary(Circuit().c4x(0, 1, 2, 3, 4)),
                       expected)
Ejemplo n.º 6
0
def test_mcx_gray_0():
    assert np.allclose(circuit_to_unitary(Circuit().mcx_gray([], 0)),
                       np.array([[0, 1], [1, 0]]))
Ejemplo n.º 7
0
def test_mcz_gray_4():
    assert np.allclose(circuit_to_unitary(Circuit().mcz_gray([0, 1, 2, 3], 4)),
                       np.diag([1] * 31 + [-1]))
Ejemplo n.º 8
0
def test_c3x():
    expected = np.eye(16)
    expected[7, 7] = expected[15, 15] = 0
    expected[7, 15] = expected[15, 7] = 1
    assert np.allclose(circuit_to_unitary(Circuit().c3x(0, 1, 2, 3)), expected)
Ejemplo n.º 9
0
def test_mcx_with_ancilla_1():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcx_with_ancilla([1], 2, 0)),
        circuit_to_unitary(Circuit().cx[1, 2]))
Ejemplo n.º 10
0
def test_mcx_with_ancilla_5():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcx_with_ancilla([1, 2, 3, 4, 5], 6, 0)),
        circuit_to_unitary(Circuit().mcx_gray([1, 2, 3, 4, 5], 6)))
Ejemplo n.º 11
0
def test_mcz_with_ancilla_3():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcz_with_ancilla([1, 2, 3], 4, 0)),
        circuit_to_unitary(Circuit().c3z(1, 2, 3, 4)))
Ejemplo n.º 12
0
def test_mcz_with_ancilla_6():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcz_with_ancilla([1, 2, 3, 4, 5, 6], 7,
                                                      0)),
        circuit_to_unitary(Circuit().mcz_gray([1, 2, 3, 4, 5, 6], 7)))
Ejemplo n.º 13
0
def test_mcz_with_ancilla_2():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcz_with_ancilla([1, 2], 3, 0)),
        circuit_to_unitary(Circuit().ccz[1, 2, 3]))
Ejemplo n.º 14
0
def test_c3z():
    assert np.allclose(circuit_to_unitary(Circuit().c3z(0, 1, 2, 3)),
                       np.diag([1] * 15 + [-1]))
Ejemplo n.º 15
0
def test_mcz_with_ancilla_0():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcz_with_ancilla([], 1, 0)),
        circuit_to_unitary(Circuit().z[1]))
Ejemplo n.º 16
0
def test_mcx_gray_4():
    expected = np.eye(32)
    expected[15, 15] = expected[31, 31] = 0
    expected[15, 31] = expected[31, 15] = 1
    assert np.allclose(circuit_to_unitary(Circuit().mcx_gray([0, 1, 2, 3], 4)),
                       expected)
Ejemplo n.º 17
0
def test_mcz_gray_1():
    assert np.allclose(circuit_to_unitary(Circuit().mcz_gray([0], 1)),
                       np.diag([1, 1, 1, -1]))
Ejemplo n.º 18
0
def test_c4z():
    assert np.allclose(circuit_to_unitary(Circuit().c4z(0, 1, 2, 3, 4)),
                       np.diag([1] * 31 + [-1]))
Ejemplo n.º 19
0
def test_mcx_with_ancilla_4():
    assert np.allclose(
        circuit_to_unitary(Circuit().mcx_with_ancilla([1, 2, 3, 4], 5, 0)),
        circuit_to_unitary(Circuit().c4x(1, 2, 3, 4, 5)))