예제 #1
0
파일: test_qubit.py 프로젝트: ness01/sympy
def test_matrix_to_qubits():
    assert matrix_to_qubit(Matrix([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) == Qubit(0, 0, 0, 0)
    assert qubit_to_matrix(Qubit(0, 0, 0, 0)) == Matrix([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert (
        matrix_to_qubit(sqrt(2) * 2 * Matrix([1, 1, 1, 1, 1, 1, 1, 1]))
        == (
            2
            * sqrt(2)
            * (
                Qubit(0, 0, 0)
                + Qubit(0, 0, 1)
                + Qubit(0, 1, 0)
                + Qubit(0, 1, 1)
                + Qubit(1, 0, 0)
                + Qubit(1, 0, 1)
                + Qubit(1, 1, 0)
                + Qubit(1, 1, 1)
            )
        ).expand()
    )
    assert qubit_to_matrix(
        2
        * sqrt(2)
        * (
            Qubit(0, 0, 0)
            + Qubit(0, 0, 1)
            + Qubit(0, 1, 0)
            + Qubit(0, 1, 1)
            + Qubit(1, 0, 0)
            + Qubit(1, 0, 1)
            + Qubit(1, 1, 0)
            + Qubit(1, 1, 1)
        )
    ) == sqrt(2) * 2 * Matrix([1, 1, 1, 1, 1, 1, 1, 1])
예제 #2
0
def test_matrix_to_qubits():
    assert matrix_to_qubit(Matrix([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]))\
    == Qubit(0,0,0,0)
    assert qubit_to_matrix(Qubit(0,0,0,0)) ==\
    Matrix([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    assert matrix_to_qubit(sqrt(2)*2*Matrix([1,1,1,1,1,1,1,1])) ==\
    (2*sqrt(2)*(Qubit(0,0,0) + Qubit(0,0,1) + Qubit(0,1,0) + Qubit(0,1,1)\
    + Qubit(1,0,0) + Qubit(1,0,1) + Qubit(1,1,0) + Qubit(1,1,1))).expand()
    assert qubit_to_matrix(2*sqrt(2)*(Qubit(0,0,0) + Qubit(0,0,1) + Qubit(0,1,0)\
    + Qubit(0,1,1) + Qubit(1,0,0) + Qubit(1,0,1) + Qubit(1,1,0) + Qubit(1,1,1)))\
    == sqrt(2)*2*Matrix([1,1,1,1,1,1,1,1])
예제 #3
0
def test_matrix_to_qubits():
    qb = Qubit(0, 0, 0, 0)
    mat = Matrix([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert matrix_to_qubit(mat) == qb
    assert qubit_to_matrix(qb) == mat

    state = 2 * sqrt(2) * (Qubit(0, 0, 0) + Qubit(0, 0, 1) + Qubit(0, 1, 0) +
                           Qubit(0, 1, 1) + Qubit(1, 0, 0) + Qubit(1, 0, 1) +
                           Qubit(1, 1, 0) + Qubit(1, 1, 1))
    ones = sqrt(2) * 2 * Matrix([1, 1, 1, 1, 1, 1, 1, 1])
    assert matrix_to_qubit(ones) == state.expand()
    assert qubit_to_matrix(state) == ones
예제 #4
0
def test_matrix_to_qubits():
    qb = Qubit(0, 0, 0, 0)
    mat = Matrix([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    assert matrix_to_qubit(mat) == qb
    assert qubit_to_matrix(qb) == mat

    state = 2*sqrt(2)*(Qubit(0, 0, 0) + Qubit(0, 0, 1) + Qubit(0, 1, 0) +
                       Qubit(0, 1, 1) + Qubit(1, 0, 0) + Qubit(1, 0, 1) +
                       Qubit(1, 1, 0) + Qubit(1, 1, 1))
    ones = sqrt(2)*2*Matrix([1, 1, 1, 1, 1, 1, 1, 1])
    assert matrix_to_qubit(ones) == state.expand()
    assert qubit_to_matrix(state) == ones
예제 #5
0
def test_UGate():
    a, b, c, d = symbols("a,b,c,d")
    uMat = Matrix([[a, b], [c, d]])

    # Test basic case where gate exists in 1-qubit space
    u1 = UGate((0,), uMat)
    assert represent(u1, nqubits=1) == uMat
    assert qapply(u1 * Qubit("0")) == a * Qubit("0") + c * Qubit("1")
    assert qapply(u1 * Qubit("1")) == b * Qubit("0") + d * Qubit("1")

    # Test case where gate exists in a larger space
    u2 = UGate((1,), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep * qubit_to_matrix(IntQubit(i, 2)) == qubit_to_matrix(qapply(u2 * IntQubit(i, 2)))
def test_UGate():
    a, b, c, d = symbols('a,b,c,d')
    uMat = Matrix([[a, b], [c, d]])

    # Test basic case where gate exists in 1-qubit space
    u1 = UGate((0, ), uMat)
    assert represent(u1, nqubits=1) == uMat
    assert qapply(u1 * Qubit('0')) == a * Qubit('0') + c * Qubit('1')
    assert qapply(u1 * Qubit('1')) == b * Qubit('0') + d * Qubit('1')

    # Test case where gate exists in a larger space
    u2 = UGate((1, ), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep*qubit_to_matrix(IntQubit(i, 2)) == \
            qubit_to_matrix(qapply(u2*IntQubit(i, 2)))
예제 #7
0
def test_ugate():
    """Test the general UGate."""
    a,b,c,d = symbols('abcd')
    uMat = Matrix([[a,b],[c,d]])

    # Test basic case where gate exists in 1-qubit space
    u1 = UGate((0,), uMat)
    assert represent(u1, nqubits = 1) == uMat
    assert apply_operators(u1*Qubit('0')) == a*Qubit('0') + c*Qubit('1')
    assert apply_operators(u1*Qubit('1')) == b*Qubit('0') + d*Qubit('1')

    # Test case where gate exists in a larger space
    u2 = UGate((1,), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep*qubit_to_matrix(IntQubit(i,2)) ==\
            qubit_to_matrix(apply_operators(u2*IntQubit(i,2)))
예제 #8
0
def test_UGate_CGate_combo():
    a, b, c, d = symbols("a,b,c,d")
    uMat = Matrix([[a, b], [c, d]])
    cMat = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, a, b], [0, 0, c, d]])

    # Test basic case where gate exists in 1-qubit space.
    u1 = UGate((0,), uMat)
    cu1 = CGate(1, u1)
    assert represent(cu1, nqubits=2) == cMat
    assert qapply(cu1 * Qubit("10")) == a * Qubit("10") + c * Qubit("11")
    assert qapply(cu1 * Qubit("11")) == b * Qubit("10") + d * Qubit("11")
    assert qapply(cu1 * Qubit("01")) == Qubit("01")
    assert qapply(cu1 * Qubit("00")) == Qubit("00")

    # Test case where gate exists in a larger space.
    u2 = UGate((1,), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep * qubit_to_matrix(IntQubit(i, 2)) == qubit_to_matrix(qapply(u2 * IntQubit(i, 2)))
def test_UGate_CGate_combo():
    a, b, c, d = symbols('a,b,c,d')
    uMat = Matrix([[a, b], [c, d]])
    cMat = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, a, b], [0, 0, c, d]])

    # Test basic case where gate exists in 1-qubit space.
    u1 = UGate((0, ), uMat)
    cu1 = CGate(1, u1)
    assert represent(cu1, nqubits=2) == cMat
    assert qapply(cu1 * Qubit('10')) == a * Qubit('10') + c * Qubit('11')
    assert qapply(cu1 * Qubit('11')) == b * Qubit('10') + d * Qubit('11')
    assert qapply(cu1 * Qubit('01')) == Qubit('01')
    assert qapply(cu1 * Qubit('00')) == Qubit('00')

    # Test case where gate exists in a larger space.
    u2 = UGate((1, ), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep*qubit_to_matrix(IntQubit(i, 2)) == \
            qubit_to_matrix(qapply(u2*IntQubit(i, 2)))
예제 #10
0
def test_ugate_cgate_combo():
    """Test a UGate/CGate combination."""
    a,b,c,d = symbols('abcd')
    uMat = Matrix([[a,b],[c,d]])
    cMat = Matrix([[1,0,0,0],[0,1,0,0],[0,0,a,b],[0,0,c,d]])

    # Test basic case where gate exists in 1-qubit space.
    u1 = UGate((0,), uMat)
    cu1 = CGate(1, u1)
    assert represent(cu1, nqubits = 2) == cMat
    assert apply_operators(cu1*Qubit('10')) == a*Qubit('10') + c*Qubit('11')
    assert apply_operators(cu1*Qubit('11')) == b*Qubit('10') + d*Qubit('11')
    assert apply_operators(cu1*Qubit('01')) == Qubit('01')
    assert apply_operators(cu1*Qubit('00')) == Qubit('00')

    # Test case where gate exists in a larger space.
    u2 = UGate((1,), uMat)
    u2Rep = represent(u2, nqubits=2)
    for i in range(4):
        assert u2Rep*qubit_to_matrix(IntQubit(i,2)) ==\
            qubit_to_matrix(apply_operators(u2*IntQubit(i,2)))
예제 #11
0
def test_IntQubit():
    # issue 9136
    iqb = IntQubit(0, nqubits=1)
    assert qubit_to_matrix(Qubit('0')) == qubit_to_matrix(iqb)

    qb = Qubit('1010')
    assert qubit_to_matrix(IntQubit(qb)) == qubit_to_matrix(qb)

    iqb = IntQubit(1, nqubits=1)
    assert qubit_to_matrix(Qubit('1')) == qubit_to_matrix(iqb)
    assert qubit_to_matrix(IntQubit(1)) == qubit_to_matrix(iqb)

    iqb = IntQubit(7, nqubits=4)
    assert qubit_to_matrix(Qubit('0111')) == qubit_to_matrix(iqb)
    assert qubit_to_matrix(IntQubit(7, 4)) == qubit_to_matrix(iqb)

    iqb = IntQubit(8)
    assert iqb.as_int() == 8
    assert iqb.qubit_values == (1, 0, 0, 0)

    iqb = IntQubit(7, 4)
    assert iqb.qubit_values == (0, 1, 1, 1)
    assert IntQubit(3) == IntQubit(3, 2)

    #test Dual Classes
    iqb = IntQubit(3)
    iqb_bra = IntQubitBra(3)
    assert iqb.dual_class() == IntQubitBra
    assert iqb_bra.dual_class() == IntQubit

    iqb = IntQubit(5)
    iqb_bra = IntQubitBra(5)
    assert iqb._eval_innerproduct_IntQubitBra(iqb_bra) == Integer(1)

    iqb = IntQubit(4)
    iqb_bra = IntQubitBra(5)
    assert iqb._eval_innerproduct_IntQubitBra(iqb_bra) == Integer(0)
    raises(ValueError, lambda: IntQubit(4, 1))

    raises(ValueError, lambda: IntQubit('5'))
    raises(ValueError, lambda: IntQubit(5, '5'))
    raises(ValueError, lambda: IntQubit(5, nqubits='5'))
    raises(TypeError, lambda: IntQubit(5, bad_arg=True))
예제 #12
0
def test_IntQubit():
    # issue 9136
    iqb = IntQubit(0, nqubits=1)
    assert qubit_to_matrix(Qubit('0')) == qubit_to_matrix(iqb)

    qb = Qubit('1010')
    assert qubit_to_matrix(IntQubit(qb)) == qubit_to_matrix(qb)

    iqb = IntQubit(1, nqubits=1)
    assert qubit_to_matrix(Qubit('1')) == qubit_to_matrix(iqb)
    assert qubit_to_matrix(IntQubit(1)) == qubit_to_matrix(iqb)

    iqb = IntQubit(7, nqubits=4)
    assert qubit_to_matrix(Qubit('0111')) == qubit_to_matrix(iqb)
    assert qubit_to_matrix(IntQubit(7, 4)) == qubit_to_matrix(iqb)

    iqb = IntQubit(8)
    assert iqb.as_int() == 8
    assert iqb.qubit_values == (1, 0, 0, 0)

    iqb = IntQubit(7, 4)
    assert iqb.qubit_values == (0, 1, 1, 1)
    assert IntQubit(3) == IntQubit(3, 2)

    #test Dual Classes
    iqb = IntQubit(3)
    iqb_bra = IntQubitBra(3)
    assert iqb.dual_class() == IntQubitBra
    assert iqb_bra.dual_class() == IntQubit

    iqb = IntQubit(5)
    iqb_bra = IntQubitBra(5)
    assert iqb._eval_innerproduct_IntQubitBra(iqb_bra) == Integer(1)

    iqb = IntQubit(4)
    iqb_bra = IntQubitBra(5)
    assert iqb._eval_innerproduct_IntQubitBra(iqb_bra) == Integer(0)
    raises(ValueError, lambda: IntQubit(4, 1))

    raises(ValueError, lambda: IntQubit('5'))
    raises(ValueError, lambda: IntQubit(5, '5'))
    raises(ValueError, lambda: IntQubit(5, nqubits='5'))
    raises(TypeError, lambda: IntQubit(5, bad_arg=True))
예제 #13
0
def number_of_qubits(psi) -> int:
    return int(log2(len(qubit_to_matrix(psi))))
예제 #14
0
파일: __init__.py 프로젝트: tomekzaw/ewl
 def amplitudes(self, *, simplify: bool = True) -> Matrix:
     ampl = self.J_H @ TensorProduct(*self.players) @ qubit_to_matrix(
         self.psi)
     if simplify:
         ampl = ampl.applyfunc(convert_exp_to_trig)
     return ampl
예제 #15
0
파일: __init__.py 프로젝트: tomekzaw/ewl
 def J(self) -> Matrix:
     return Matrix.hstack(*[
         TensorProduct(*base) @ qubit_to_matrix(self.psi)
         for base in product((self.C, self.D),
                             repeat=number_of_qubits(self.psi))
     ])