Ejemplo n.º 1
0
def test_ID_matrix():
    from shor.gates import ID
    g = ID(0)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0], [0, 1]]))
Ejemplo n.º 2
0
def test_sdg_matrix():
    from shor.gates import Sdg
    g = Sdg(0)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0], [0, -1j]]))
Ejemplo n.º 3
0
def test_tdg_matrix():
    from shor.gates import Tdg
    g = Tdg(0)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.allclose(g.to_matrix(),
                       np.array([[1, 0], [0, np.exp(-1j * np.pi / 4)]]))
Ejemplo n.º 4
0
def test_paulix_matrix():
    from shor.gates import PauliX, X
    gates = [PauliX(), X()]

    for g in gates:
        assert is_square(g.to_matrix())
        assert is_unitary(g.to_matrix())
        assert np.array_equal(g.to_matrix(), np.array([[0, 1], [1, 0]]))
Ejemplo n.º 5
0
def test_cx_matrix():
    from shor.gates import Cx

    g = Cx(0, 1)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]]))
Ejemplo n.º 6
0
def test_cy_matrix():
    from shor.gates import CY

    g = CY(0, 1)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, -1j], [0, 0, 1j, 0]]))
Ejemplo n.º 7
0
def test_swap_matrix():
    from shor.gates import SWAP

    g = SWAP(0, 1)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]))
Ejemplo n.º 8
0
def test_u1_matrix():
    from shor.gates import U1
    angle = math.pi / 8
    g = U1(0, angle=math.pi / 8)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(),
                          np.array([[1, 0], [0, np.exp(1j * angle)]]))
Ejemplo n.º 9
0
def test_hadamard_matrix():
    from shor.gates import H, Hadamard

    gates = [Hadamard(), H()]

    for g in gates:
        assert is_square(g.to_matrix())
        assert is_unitary(g.to_matrix())
        assert np.array_equal(g.to_matrix(), np.multiply(np.divide(1, np.sqrt(2)), np.array([[1, 1], [1, -1]])))
Ejemplo n.º 10
0
def test_qft_matrix():
    from shor.gates import QFT

    g = QFT(0, 1)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(), np.multiply(1 / 2, np.array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]]))
    )
Ejemplo n.º 11
0
def test_ch_matrix():
    from shor.gates import CH
    g = CH(0, 1)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array([[1, 0, 0, 0], [0, 1, 0, 0],
                  [0, 0, 1 / np.sqrt(2), 1 / np.sqrt(2)],
                  [0, 0, 1 / np.sqrt(2), -1 / np.sqrt(2)]]))
Ejemplo n.º 12
0
def test_crz_matrix():
    from shor.gates import CRZ
    angle = np.pi / 3
    g = CRZ(0, 1, angle=np.pi / 3)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array([[1, 0, 0, 0], [0, np.exp(-1j * angle / 2), 0, 0],
                  [0, 0, 1, 0], [0, 0, 0, np.exp(1j * angle / 2)]]))
Ejemplo n.º 13
0
def test_rz_matrix():
    from shor.gates import Rz
    angle = math.pi / 4
    g = Rz(0, angle=math.pi / 4)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array([[np.exp(-(1 / 2) * 1j * angle), 0],
                  [0, np.exp((1 / 2) * 1j * angle)]]))
Ejemplo n.º 14
0
def test_cnot_matrix():
    from shor.gates import CNOT

    g = CNOT()

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(g.to_matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]))

    # Try with parameter
    CNOT(1, 0)
Ejemplo n.º 15
0
def test_cr_matrix():
    from shor.gates import Cr

    angle = np.pi / 2
    g = Cr(0, 1, angle=np.pi / 2)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, np.exp(1j * angle)]])
    )
Ejemplo n.º 16
0
def test_crk_matrix():
    from shor.gates import CRk

    k = 2
    g = CRk(0, 1, k=2)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, np.exp((2 * np.pi * 1j) / (2 ** k))]]),
    )
Ejemplo n.º 17
0
def test_ccnot_matrix():
    from shor.gates import CCNOT
    g = CCNOT(0, 1, 2)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0],
                  [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0],
                  [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0]]))
Ejemplo n.º 18
0
def test_ry():
    from shor.gates import Ry
    angle = math.pi / 8
    g = Ry(0, angle=math.pi / 8)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())

    assert np.array_equal(
        g.to_matrix(),
        np.array([[math.cos(angle / 2), -math.sin(angle / 2)],
                  [math.sin(angle / 2),
                   math.cos(angle / 2)]]))
Ejemplo n.º 19
0
def test_cswap_matrix():
    from shor.gates import CSWAP, Fredkin
    gate1 = CSWAP()
    gate2 = Fredkin()

    assert gate1.__class__ == gate2.__class__

    assert is_square(gate1.to_matrix())
    assert is_unitary(gate1.to_matrix())

    expected = np.eye(8)
    expected[:, [5, 6]] = expected[:, [6, 5]]
    assert np.array_equal(gate1.to_matrix(), expected)

    # Try with parameter
    CSWAP(2, 0, 1)
Ejemplo n.º 20
0
def test_u3_matrix():
    from shor.gates import U3
    theta = np.pi / 2
    phi = -np.pi / 3
    alpha = np.pi / 2

    g = U3(0, theta=np.pi / 2, phi=-np.pi / 3, alpha=np.pi / 2)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array(
            [[np.cos(theta / 2), -np.exp(1j * alpha) * math.sin(theta / 2)],
             [
                 np.exp(1j * phi) * math.sin(theta / 2),
                 np.exp(1j * (phi + alpha)) * math.cos(theta / 2)
             ]]))
Ejemplo n.º 21
0
def test_u2_matrix():
    from shor.gates import U2
    phi = -np.pi / 3
    alpha = np.pi / 2
    theta = np.pi / 2

    g = U2(0, phi=-np.pi / 3, alpha=np.pi / 2)

    assert is_square(g.to_matrix())
    assert is_unitary(g.to_matrix())
    assert np.array_equal(
        g.to_matrix(),
        np.array(
            [[np.cos(theta / 2), -np.exp(1j * alpha) * math.sin(theta / 2)],
             [
                 np.exp(1j * phi) * math.sin(theta / 2),
                 np.exp(1j * (phi + alpha)) * math.cos(theta / 2)
             ]]))
    assert np.allclose(g.to_matrix(), (1 / np.sqrt(2)) * np.array([[
        1, -np.exp(1j * alpha)
    ], [np.exp(1j * phi), np.exp(1j * (phi + alpha))]]))