def test_cnot_gate():
    """Test the CNOT gate."""
    circuit = CNotGate(1, 0)
    assert represent(circuit, nqubits=2) == \
        Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
    circuit = circuit * Qubit('111')
    assert matrix_to_qubit(represent(circuit, nqubits=3)) == \
        qapply(circuit)

    circuit = CNotGate(1, 0)
    assert Dagger(circuit) == circuit
    assert Dagger(Dagger(circuit)) == circuit
    assert circuit * circuit == 1
Пример #2
0
def test_gate():
    a, b, c, d = symbols('a,b,c,d')
    uMat = Matrix([[a, b], [c, d]])
    q = Qubit(1, 0, 1, 0, 1)
    g1 = IdentityGate(2)
    g2 = CGate((3, 0), XGate(1))
    g3 = CNotGate(1, 0)
    g4 = UGate((0, ), uMat)
    assert str(g1) == '1(2)'
    assert pretty(g1) == '1 \n 2'
    assert upretty(g1) == u'1 \n 2'
    assert latex(g1) == r'1_{2}'
    sT(g1, "IdentityGate(Integer(2))")
    assert str(g1 * q) == '1(2)*|10101>'
    ascii_str = \
"""\
1 *|10101>\n\
 2        \
"""
    ucode_str = \
u"""\
1 ⋅❘10101⟩\n\
 2        \
"""
    assert pretty(g1 * q) == ascii_str
    assert upretty(g1 * q) == ucode_str
    assert latex(g1 * q) == r'1_{2} {\left|10101\right\rangle }'
    sT(
        g1 * q,
        "Mul(IdentityGate(Integer(2)), Qubit(Integer(1),Integer(0),Integer(1),Integer(0),Integer(1)))"
    )
    assert str(g2) == 'C((3,0),X(1))'
    ascii_str = \
"""\
C   /X \\\n\
 3,0\\ 1/\
"""
    ucode_str = \
u"""\
C   ⎛X ⎞\n\
 3,0⎝ 1⎠\
"""
    assert pretty(g2) == ascii_str
    assert upretty(g2) == ucode_str
    assert latex(g2) == r'C_{3,0}{\left(X_{1}\right)}'
    sT(g2, "CGate(Tuple(Integer(3), Integer(0)),XGate(Integer(1)))")
    assert str(g3) == 'CNOT(1,0)'
    ascii_str = \
"""\
CNOT   \n\
    1,0\
"""
    ucode_str = \
u"""\
CNOT   \n\
    1,0\
"""
    assert pretty(g3) == ascii_str
    assert upretty(g3) == ucode_str
    assert latex(g3) == r'CNOT_{1,0}'
    sT(g3, "CNotGate(Integer(1),Integer(0))")
    # str(g4) Fails
    #assert str(g4) == ''
    ascii_str = \
"""\
U \n\
 0\
"""
    ucode_str = \
u"""\
U \n\
 0\
"""
    assert pretty(g4) == ascii_str
    assert upretty(g4) == ucode_str
    assert latex(g4) == r'U_{0}'
    sT(
        g4,
        "UGate(Tuple(Integer(0)),MutableDenseMatrix([[Symbol('a'), Symbol('b')], [Symbol('c'), Symbol('d')]]))"
    )
Пример #3
0
def test_sympy__physics__quantum__gate__CNotGate():
    from sympy.physics.quantum.gate import CNotGate
    assert _test_args(CNotGate(0, 1))
Пример #4
0
def test_gate():
    a, b, c, d = symbols("a,b,c,d")
    uMat = Matrix([[a, b], [c, d]])
    q = Qubit(1, 0, 1, 0, 1)
    g1 = IdentityGate(2)
    g2 = CGate((3, 0), XGate(1))
    g3 = CNotGate(1, 0)
    g4 = UGate((0, ), uMat)
    assert str(g1) == "1(2)"
    assert pretty(g1) == "1 \n 2"
    assert upretty(g1) == u"1 \n 2"
    assert latex(g1) == r"1_{2}"
    sT(g1, "IdentityGate(Integer(2))")
    assert str(g1 * q) == "1(2)*|10101>"
    ascii_str = """\
1 *|10101>\n\
 2        \
"""
    ucode_str = u("""\
1 ⋅❘10101⟩\n\
 2        \
""")
    assert pretty(g1 * q) == ascii_str
    assert upretty(g1 * q) == ucode_str
    assert latex(g1 * q) == r"1_{2} {\left|10101\right\rangle }"
    sT(
        g1 * q,
        "Mul(IdentityGate(Integer(2)), Qubit(Integer(1),Integer(0),Integer(1),Integer(0),Integer(1)))",
    )
    assert str(g2) == "C((3,0),X(1))"
    ascii_str = """\
C   /X \\\n\
 3,0\\ 1/\
"""
    ucode_str = u("""\
C   ⎛X ⎞\n\
 3,0⎝ 1⎠\
""")
    assert pretty(g2) == ascii_str
    assert upretty(g2) == ucode_str
    assert latex(g2) == r"C_{3,0}{\left(X_{1}\right)}"
    sT(g2, "CGate(Tuple(Integer(3), Integer(0)),XGate(Integer(1)))")
    assert str(g3) == "CNOT(1,0)"
    ascii_str = """\
CNOT   \n\
    1,0\
"""
    ucode_str = u("""\
CNOT   \n\
    1,0\
""")
    assert pretty(g3) == ascii_str
    assert upretty(g3) == ucode_str
    assert latex(g3) == r"CNOT_{1,0}"
    sT(g3, "CNotGate(Integer(1),Integer(0))")
    ascii_str = """\
U \n\
 0\
"""
    ucode_str = u("""\
U \n\
 0\
""")
    assert (str(g4) == """\
U((0,),Matrix([\n\
[a, b],\n\
[c, d]]))\
""")
    assert pretty(g4) == ascii_str
    assert upretty(g4) == ucode_str
    assert latex(g4) == r"U_{0}"
    sT(
        g4,
        "UGate(Tuple(Integer(0)),MutableDenseMatrix([[Symbol('a'), Symbol('b')], [Symbol('c'), Symbol('d')]]))",
    )