Example #1
0
def test_quantum_fourier():
    assert QFT(0,3).decompose() == SwapGate(0,2)*HadamardGate(0)*CGate((0,), PhaseGate(1))\
    *HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2))*HadamardGate(2)

    assert IQFT(0,3).decompose() == HadamardGate(2)*CGate((1,), RkGate(2,-2))*CGate((0,),RkGate(2,-3))\
    *HadamardGate(1)*CGate((0,), RkGate(1,-2))*HadamardGate(0)*SwapGate(0,2)

    assert represent(QFT(0,3), nqubits=3)\
     == Matrix([[exp(2*pi*I/8)**(i*j%8)/sqrt(8) for i in range(8)] for j in range(8)])

    assert QFT(0, 4).decompose()  #non-trivial decomposition
    assert qapply(QFT(0,3).decompose()*Qubit(0,0,0)).expand() ==\
    qapply(HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0,0,0)).expand()
def test_generate_gate_rules_2():
    # Test with Muls
    (x, y, z, h) = create_gate_sequence()
    ph = PhaseGate(0)
    cgate_t = CGate(0, TGate(1))

    # Note: 1 (type int) is not the same as 1 (type One)
    expected = {(x, Integer(1))}
    assert generate_gate_rules((x, ), return_as_muls=True) == expected

    expected = {(Integer(1), Integer(1))}
    assert generate_gate_rules(x * x, return_as_muls=True) == expected

    expected = {((), ())}
    assert generate_gate_rules(x * x, return_as_muls=False) == expected

    gate_rules = {(x * y * x, Integer(1)), (y, Integer(1)), (y * x, x),
                  (x * y, x)}
    assert generate_gate_rules(x * y * x, return_as_muls=True) == gate_rules

    gate_rules = {(x * y * z, Integer(1)), (y * z * x, Integer(1)),
                  (z * x * y, Integer(1)), (Integer(1), x * z * y),
                  (Integer(1), y * x * z), (Integer(1), z * y * x), (x, z * y),
                  (y * z, x), (y, x * z), (z * x, y), (z, y * x), (x * y, z)}
    actual = generate_gate_rules(x * y * z, return_as_muls=True)
    assert actual == gate_rules

    gate_rules = {(Integer(1), h * z * y * x), (Integer(1), x * h * z * y),
                  (Integer(1), y * x * h * z), (Integer(1), z * y * x * h),
                  (h, z * y * x), (x, h * z * y), (y, x * h * z),
                  (z, y * x * h), (h * x, z * y), (z * h, y * x),
                  (x * y, h * z), (y * z, x * h), (h * x * y, z),
                  (x * y * z, h), (y * z * h, x), (z * h * x, y),
                  (h * x * y * z, Integer(1)), (x * y * z * h, Integer(1)),
                  (y * z * h * x, Integer(1)), (z * h * x * y, Integer(1))}
    actual = generate_gate_rules(x * y * z * h, return_as_muls=True)
    assert actual == gate_rules

    gate_rules = {(Integer(1), cgate_t**(-1) * ph**(-1) * x),
                  (Integer(1), ph**(-1) * x * cgate_t**(-1)),
                  (Integer(1), x * cgate_t**(-1) * ph**(-1)),
                  (cgate_t, ph**(-1) * x), (ph, x * cgate_t**(-1)),
                  (x, cgate_t**(-1) * ph**(-1)), (cgate_t * x, ph**(-1)),
                  (ph * cgate_t, x), (x * ph, cgate_t**(-1)),
                  (cgate_t * x * ph, Integer(1)),
                  (ph * cgate_t * x, Integer(1)),
                  (x * ph * cgate_t, Integer(1))}
    actual = generate_gate_rules(x * ph * cgate_t, return_as_muls=True)
    assert actual == gate_rules

    gate_rules = {((), (cgate_t**(-1), ph**(-1), x)),
                  ((), (ph**(-1), x, cgate_t**(-1))),
                  ((), (x, cgate_t**(-1), ph**(-1))),
                  ((cgate_t, ), (ph**(-1), x)), ((ph, ), (x, cgate_t**(-1))),
                  ((x, ), (cgate_t**(-1), ph**(-1))),
                  ((cgate_t, x), (ph**(-1), )), ((ph, cgate_t), (x, )),
                  ((x, ph), (cgate_t**(-1), )), ((cgate_t, x, ph), ()),
                  ((ph, cgate_t, x), ()), ((x, ph, cgate_t), ())}
    actual = generate_gate_rules(x * ph * cgate_t)
    assert actual == gate_rules
Example #3
0
def test_apply_represent_equality():
    gates = [
        HadamardGate(int(3 * random.random())),
        XGate(int(3 * random.random())),
        ZGate(int(3 * random.random())),
        YGate(int(3 * random.random())),
        ZGate(int(3 * random.random())),
        PhaseGate(int(3 * random.random())),
    ]

    circuit = Qubit(
        int(random.random() * 2),
        int(random.random() * 2),
        int(random.random() * 2),
        int(random.random() * 2),
        int(random.random() * 2),
        int(random.random() * 2),
    )
    for i in range(int(random.random() * 6)):
        circuit = gates[int(random.random() * 6)] * circuit

    mat = represent(circuit, nqubits=6)
    states = qapply(circuit)
    state_rep = matrix_to_qubit(mat)
    states = states.expand()
    state_rep = state_rep.expand()
    assert state_rep == states
Example #4
0
def test_generate_gate_rules_1():
    # Test with tuples
    (x, y, z, h) = create_gate_sequence()
    ph = PhaseGate(0)
    cgate_t = CGate(0, TGate(1))

    assert generate_gate_rules((x, )) == {((x, ), ())}

    gate_rules = set([((x, x), ()), ((x, ), (x, ))])
    assert generate_gate_rules((x, x)) == gate_rules

    gate_rules = set([((x, y, x), ()), ((y, x, x), ()), ((x, x, y), ()),
                      ((y, x), (x, )), ((x, y), (x, )), ((y, ), (x, x))])
    assert generate_gate_rules((x, y, x)) == gate_rules

    gate_rules = set([((x, y, z), ()), ((y, z, x), ()), ((z, x, y), ()),
                      ((), (x, z, y)), ((), (y, x, z)), ((), (z, y, x)),
                      ((x, ), (z, y)), ((y, z), (x, )), ((y, ), (x, z)),
                      ((z, x), (y, )), ((z, ), (y, x)), ((x, y), (z, ))])
    actual = generate_gate_rules((x, y, z))
    assert actual == gate_rules

    gate_rules = set([
        ((), (h, z, y, x)), ((), (x, h, z, y)), ((), (y, x, h, z)),
        ((), (z, y, x, h)), ((h, ), (z, y, x)), ((x, ), (h, z, y)),
        ((y, ), (x, h, z)), ((z, ), (y, x, h)), ((h, x), (z, y)),
        ((x, y), (h, z)), ((y, z), (x, h)), ((z, h), (y, x)),
        ((h, x, y), (z, )), ((x, y, z), (h, )), ((y, z, h), (x, )),
        ((z, h, x), (y, )), ((h, x, y, z), ()), ((x, y, z, h), ()),
        ((y, z, h, x), ()), ((z, h, x, y), ())
    ])
    actual = generate_gate_rules((x, y, z, h))
    assert actual == gate_rules

    gate_rules = set([((), (cgate_t**(-1), ph**(-1), x)),
                      ((), (ph**(-1), x, cgate_t**(-1))),
                      ((), (x, cgate_t**(-1), ph**(-1))),
                      ((cgate_t, ), (ph**(-1), x)),
                      ((ph, ), (x, cgate_t**(-1))),
                      ((x, ), (cgate_t**(-1), ph**(-1))),
                      ((cgate_t, x), (ph**(-1), )), ((ph, cgate_t), (x, )),
                      ((x, ph), (cgate_t**(-1), )), ((cgate_t, x, ph), ()),
                      ((ph, cgate_t, x), ()), ((x, ph, cgate_t), ())])
    actual = generate_gate_rules((x, ph, cgate_t))
    assert actual == gate_rules

    gate_rules = set([(Integer(1), cgate_t**(-1) * ph**(-1) * x),
                      (Integer(1), ph**(-1) * x * cgate_t**(-1)),
                      (Integer(1), x * cgate_t**(-1) * ph**(-1)),
                      (cgate_t, ph**(-1) * x), (ph, x * cgate_t**(-1)),
                      (x, cgate_t**(-1) * ph**(-1)), (cgate_t * x, ph**(-1)),
                      (ph * cgate_t, x), (x * ph, cgate_t**(-1)),
                      (cgate_t * x * ph, Integer(1)),
                      (ph * cgate_t * x, Integer(1)),
                      (x * ph * cgate_t, Integer(1))])
    actual = generate_gate_rules((x, ph, cgate_t), return_as_muls=True)
    assert actual == gate_rules
def test_bfs_identity_search_xfail():
    scipy = import_module('scipy', __import__kwargs={'fromlist': ['sparse']})
    if scipy:
        skip("scipy installed.")
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set
Example #6
0
def test_RkGate():
    x = Symbol('x')
    assert RkGate(1, x).k == x
    assert RkGate(1, x).targets == (1, )
    assert RkGate(1, 1) == ZGate(1)
    assert RkGate(2, 2) == PhaseGate(2)
    assert RkGate(3, 3) == TGate(3)

    assert represent(RkGate(0,x), nqubits =1) ==\
    Matrix([[1,0],[0,exp(2*I*pi/2**x)]])
Example #7
0
def test_cgate():
    """Test the general CGate."""
    # Test single control functionality
    CNOTMatrix = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1],
                         [0, 0, 1, 0]])
    assert represent(CGate(1, XGate(0)), nqubits=2) == CNOTMatrix

    # Test multiple control bit functionality
    ToffoliGate = CGate((1, 2), XGate(0))
    assert represent(ToffoliGate, nqubits=3) == Matrix([
        [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],
    ])

    ToffoliGate = CGate((3, 0), XGate(1))
    assert qapply(ToffoliGate * Qubit("1001")) == matrix_to_qubit(
        represent(ToffoliGate * Qubit("1001"), nqubits=4))
    assert qapply(ToffoliGate * Qubit("0000")) == matrix_to_qubit(
        represent(ToffoliGate * Qubit("0000"), nqubits=4))

    CYGate = CGate(1, YGate(0))
    CYGate_matrix = Matrix(
        ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 0, -I), (0, 0, I, 0)))
    # Test 2 qubit controlled-Y gate decompose method.
    assert represent(CYGate.decompose(), nqubits=2) == CYGate_matrix

    CZGate = CGate(0, ZGate(1))
    CZGate_matrix = Matrix(
        ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, -1)))
    assert qapply(CZGate * Qubit("11")) == -Qubit("11")
    assert matrix_to_qubit(represent(CZGate * Qubit("11"),
                                     nqubits=2)) == -Qubit("11")
    # Test 2 qubit controlled-Z gate decompose method.
    assert represent(CZGate.decompose(), nqubits=2) == CZGate_matrix

    CPhaseGate = CGate(0, PhaseGate(1))
    assert qapply(CPhaseGate * Qubit("11")) == I * Qubit("11")
    assert matrix_to_qubit(represent(CPhaseGate * Qubit("11"),
                                     nqubits=2)) == I * Qubit("11")

    # Test that the dagger, inverse, and power of CGate is evaluated properly
    assert Dagger(CZGate) == CZGate
    assert pow(CZGate, 1) == Dagger(CZGate)
    assert Dagger(CZGate) == CZGate.inverse()
    assert Dagger(CPhaseGate) != CPhaseGate
    assert Dagger(CPhaseGate) == CPhaseGate.inverse()
    assert Dagger(CPhaseGate) == pow(CPhaseGate, -1)
    assert pow(CPhaseGate, -1) == CPhaseGate.inverse()
Example #8
0
 def __new__(cls, *args):
     if len(args) != 2:
         raise QuantumError("Rk gates only take two arguments, got: %r" % args)
     # For small k, Rk gates simplify to other gates, using these
     # substitutions give us familiar results for the QFT for small numbers
     # of qubits.
     target = args[0]
     k = args[1]
     if k == 1:
         return ZGate(target)
     elif k == 2:
         return PhaseGate(target)
     elif k == 3:
         return TGate(target)
     args = cls._eval_args(args)
     inst = Expr.__new__(cls, *args)
     inst.hilbert_space = cls._eval_hilbert_space(args)
     return inst
Example #9
0
def test_cgate():
    """Test the general CGate."""
    # Test single control functionality
    CNOTMatrix = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1],
                         [0, 0, 1, 0]])
    assert represent(CGate(1, XGate(0)), nqubits=2) == CNOTMatrix

    # Test multiple control bit functionality
    ToffoliGate = CGate((1, 2), XGate(0))
    assert represent(ToffoliGate, nqubits=3) == \
    Matrix([[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]])

    ToffoliGate = CGate((3, 0), XGate(1))
    assert qapply(ToffoliGate*Qubit('1001')) == \
    matrix_to_qubit(represent(ToffoliGate*Qubit('1001'), nqubits=4))
    assert qapply(ToffoliGate*Qubit('0000')) == \
    matrix_to_qubit(represent(ToffoliGate*Qubit('0000'), nqubits=4))

    CYGate = CGate(1, YGate(0))
    CYGate_matrix = Matrix(
        ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 0, -I), (0, 0, I, 0)))
    # Test 2 qubit controlled-Y gate decompose method.
    assert represent(CYGate.decompose(), nqubits=2) == CYGate_matrix

    CZGate = CGate(0, ZGate(1))
    CZGate_matrix = Matrix(
        ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, -1)))
    assert qapply(CZGate * Qubit('11')) == -Qubit('11')
    assert matrix_to_qubit(represent(CZGate*Qubit('11'),nqubits=2)) ==\
        -Qubit('11')
    # Test 2 qubit controlled-Z gate decompose method.
    assert represent(CZGate.decompose(), nqubits=2) == CZGate_matrix

    CPhaseGate = CGate(0, PhaseGate(1))
    assert qapply(CPhaseGate*Qubit('11')) ==\
        I*Qubit('11')
    assert matrix_to_qubit(represent(CPhaseGate*Qubit('11'), nqubits=2)) == \
        I*Qubit('11')
def test_represent_phasegate():
    """Test the representation of the S gate."""
    circuit = PhaseGate(0) * Qubit('01')
    answer = represent(circuit, nqubits=2)
    assert Matrix([0, I, 0, 0]) == answer
Example #11
0
def test_sympy__physics__quantum__gate__PhaseGate():
    from sympy.physics.quantum.gate import PhaseGate
    assert _test_args(PhaseGate(0))
def test_bfs_identity_search_xfail():
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set
def test_bfs_identity_search():
    assert bfs_identity_search([], 1) == set()

    (x, y, z, h) = create_gate_sequence()

    gate_list = [x]
    id_set = {GateIdentity(x, x)}
    assert bfs_identity_search(gate_list, 1, max_depth=2) == id_set

    # Set should not contain degenerate quantum circuits
    gate_list = [x, y, z]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(x, y, z)
    }
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(y, z, y, z)
    }
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    gate_list = [x, y, z, h]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(x, h, z, h),
        GateIdentity(y, z, y, z),
        GateIdentity(y, h, y, h)
    }
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h)
    }
    assert id_set == bfs_identity_search(gate_list,
                                         1,
                                         max_depth=3,
                                         identity_only=True)

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(x, h, z, h),
        GateIdentity(y, z, y, z),
        GateIdentity(y, h, y, h),
        GateIdentity(x, y, h, x, h),
        GateIdentity(x, z, h, y, h),
        GateIdentity(y, z, h, z, h)
    }
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, h, z, h)
    }
    assert id_set == bfs_identity_search(gate_list,
                                         1,
                                         max_depth=4,
                                         identity_only=True)

    cnot = CNOT(1, 0)
    gate_list = [x, cnot]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(cnot, cnot),
        GateIdentity(x, cnot, x, cnot)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_x = CGate((1, ), x)
    gate_list = [x, cgate_x]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(cgate_x, cgate_x),
        GateIdentity(x, cgate_x, x, cgate_x)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_z = CGate((0, ), Z(1))
    gate_list = [cnot, cgate_z, h]
    id_set = {
        GateIdentity(h, h),
        GateIdentity(cgate_z, cgate_z),
        GateIdentity(cnot, cnot),
        GateIdentity(cnot, h, cgate_z, h)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [s, t]
    id_set = {GateIdentity(s, s, s, s)}
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set