Esempio n. 1
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()
Esempio n. 2
0
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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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
Esempio n. 3
0
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 = set([(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 = set([(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 = set([(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 = 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

    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
Esempio n. 4
0
 def ControlledGate(ctrls, target):
     return CGate(tuple(ctrls), onequbitgate(target))
    def get_sym_op(name, qid_tuple, params=None):
        """ return the sympy version for the gate
        Args:
            name (str): gate name
            qid_tuple (tuple): the ids of the qubits being operated on
            params (list): optional parameter lists, which may be needed by the U gates.
        Returns:
            object: (the sympy representation of) the gate being applied to the qubits
        Raises:
            Exception: if an unsupported operation is seen
        """
        the_gate = None
        if name == 'ID':
            the_gate = IdentityGate(*qid_tuple)  # de-tuple means unpacking
        elif name == 'X':
            the_gate = X(*qid_tuple)
        elif name == 'Y':
            the_gate = Y(*qid_tuple)
        elif name == 'Z':
            the_gate = Z(*qid_tuple)
        elif name == 'H':
            the_gate = H(*qid_tuple)
        elif name == 'S':
            the_gate = S(*qid_tuple)
        elif name == 'SDG':
            the_gate = SDGGate(*qid_tuple)
        elif name == 'T':
            the_gate = T(*qid_tuple)
        elif name == 'TDG':
            the_gate = TDGGate(*qid_tuple)
        elif name == 'CX' or name == 'CNOT':
            the_gate = CNOT(*qid_tuple)
        elif name == 'CY':
            the_gate = CGate(qid_tuple[0], Y(qid_tuple[1]))  # qid_tuple: control target
        elif name == 'CZ':
            the_gate = CGate(qid_tuple[0], Z(qid_tuple[1]))  # qid_tuple: control target
        elif name == 'CCX' or name == 'CCNOT' or name == 'TOFFOLI':
            the_gate = CGate((qid_tuple[0], qid_tuple[1]), X(qid_tuple[2]))

        if the_gate is not None:
            return the_gate

        # U gate, CU gate handled below
        if name.startswith('U') or name.startswith('CU'):
            parameters = params

            if len(parameters) == 1:  # [theta=0, phi=0, lambda]
                parameters.insert(0, 0.0)
                parameters.insert(0, 0.0)
            elif len(parameters) == 2:  # [theta=pi/2, phi, lambda]
                parameters.insert(0, pi/2)
            elif len(parameters) == 3:  # [theta, phi, lambda]
                pass
            else:
                raise Exception('U gate must carry 1, 2 or 3 parameters!')

            if name.startswith('U'):
                ugate = UGateGeneric(*qid_tuple)
                u_mat = compute_ugate_matrix(parameters)
                ugate.set_target_matrix(u_matrix=u_mat)
                return ugate

            elif name.startswith('CU'):  # additional treatment for CU1, CU2, CU3
                ugate = UGateGeneric(*qid_tuple)
                u_mat = compute_ugate_matrix(parameters)
                ugate.set_target_matrix(u_matrix=u_mat)
                return CGate(qid_tuple[0], ugate)
        # if the control flow comes here,  alarm!
        raise Exception('Not supported')
Esempio n. 6
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()
Esempio n. 7
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')]]))"
    )
Esempio n. 8
0
def test_qasm_ex2():
    q = Qasm('qubit q_0', 'qubit q_1', 'qubit q_2', 'h  q_1',
             'cnot q_1,q_2', 'cnot q_0,q_1', 'h q_0',
             'measure q_1', 'measure q_0',
             'c-x q_1,q_2', 'c-z q_0,q_2')
    assert q.get_circuit() == CGate(2,Z(0))*CGate(1,X(0))*Mz(2)*Mz(1)*H(2)*CNOT(2,1)*CNOT(1,0)*H(1)
Esempio n. 9
0
 def cz(self, a1, a2):
     fi, fj = self.indices([a1, a2])
     self.circuit.append(CGate(fi, Z(fj)))
Esempio n. 10
0
def test_convert_to_real_indices():
    i0 = Symbol('i0')
    i1 = Symbol('i1')

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

    x_i0 = X(i0)
    y_i0 = Y(i0)
    z_i0 = Z(i0)

    qubit_map = {i0: 0}
    args = (z_i0, y_i0, x_i0)
    expected = (z, y, x)
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected

    cnot_10 = CNOT(1, 0)
    cnot_01 = CNOT(0, 1)
    cgate_z_10 = CGate(1, Z(0))
    cgate_z_01 = CGate(0, Z(1))

    cnot_i1_i0 = CNOT(i1, i0)
    cnot_i0_i1 = CNOT(i0, i1)
    cgate_z_i1_i0 = CGate(i1, Z(i0))

    qubit_map = {i0: 0, i1: 1}
    args = (cnot_i1_i0, )
    expected = (cnot_10, )
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected

    args = (cgate_z_i1_i0, )
    expected = (cgate_z_10, )
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected

    args = (cnot_i0_i1, )
    expected = (cnot_01, )
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected

    qubit_map = {i0: 1, i1: 0}
    args = (cgate_z_i1_i0, )
    expected = (cgate_z_01, )
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected

    i2 = Symbol('i2')
    ccgate_z = CGate(i0, CGate(i1, Z(i2)))
    ccgate_x = CGate(i1, CGate(i2, X(i0)))

    qubit_map = {i0: 0, i1: 1, i2: 2}
    args = (ccgate_z, ccgate_x)
    expected = (CGate(0, CGate(1, Z(2))), CGate(1, CGate(2, X(0))))
    actual = convert_to_real_indices(Mul(*args), qubit_map)
    assert actual == expected

    qubit_map = {i0: 1, i2: 0, i1: 2}
    args = (ccgate_x, ccgate_z)
    expected = (CGate(2, CGate(0, X(1))), CGate(1, CGate(2, Z(0))))
    actual = convert_to_real_indices(args, qubit_map)
    assert actual == expected
Esempio n. 11
0
def test_convert_to_symbolic_indices():
    (x, y, z, h) = create_gate_sequence()

    i0 = Symbol('i0')
    exp_map = {i0: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices((x, ))
    assert actual == (X(i0), )
    assert act_map == exp_map

    expected = (X(i0), Y(i0), Z(i0), H(i0))
    exp_map = {i0: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices((x, y, z, h))
    assert actual == expected
    assert exp_map == act_map

    (x1, y1, z1, h1) = create_gate_sequence(1)
    i1 = Symbol('i1')

    expected = (X(i0), Y(i0), Z(i0), H(i0))
    exp_map = {i0: Integer(1)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices((x1, y1, z1, h1))
    assert actual == expected
    assert act_map == exp_map

    expected = (X(i0), Y(i0), Z(i0), H(i0), X(i1), Y(i1), Z(i1), H(i1))
    exp_map = {i0: Integer(0), i1: Integer(1)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(
        (x, y, z, h, x1, y1, z1, h1))
    assert actual == expected
    assert act_map == exp_map

    exp_map = {i0: Integer(1), i1: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(
        Mul(x1, y1, z1, h1, x, y, z, h))
    assert actual == expected
    assert act_map == exp_map

    expected = (X(i0), X(i1), Y(i0), Y(i1), Z(i0), Z(i1), H(i0), H(i1))
    exp_map = {i0: Integer(0), i1: Integer(1)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(
        Mul(x, x1, y, y1, z, z1, h, h1))
    assert actual == expected
    assert act_map == exp_map

    exp_map = {i0: Integer(1), i1: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(
        (x1, x, y1, y, z1, z, h1, h))
    assert actual == expected
    assert act_map == exp_map

    cnot_10 = CNOT(1, 0)
    cnot_01 = CNOT(0, 1)
    cgate_z_10 = CGate(1, Z(0))
    cgate_z_01 = CGate(0, Z(1))

    expected = (X(i0), X(i1), Y(i0), Y(i1), Z(i0), Z(i1), H(i0), H(i1),
                CNOT(i1, i0), CNOT(i0, i1), CGate(i1, Z(i0)), CGate(i0, Z(i1)))
    exp_map = {i0: Integer(0), i1: Integer(1)}
    args = (x, x1, y, y1, z, z1, h, h1, cnot_10, cnot_01, cgate_z_10,
            cgate_z_01)
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    args = (x1, x, y1, y, z1, z, h1, h, cnot_10, cnot_01, cgate_z_10,
            cgate_z_01)
    expected = (X(i0), X(i1), Y(i0), Y(i1), Z(i0), Z(i1), H(i0), H(i1),
                CNOT(i0, i1), CNOT(i1, i0), CGate(i0, Z(i1)), CGate(i1, Z(i0)))
    exp_map = {i0: Integer(1), i1: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    args = (cnot_10, h, cgate_z_01, h)
    expected = (CNOT(i0, i1), H(i1), CGate(i1, Z(i0)), H(i1))
    exp_map = {i0: Integer(1), i1: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    args = (cnot_01, h1, cgate_z_10, h1)
    exp_map = {i0: Integer(0), i1: Integer(1)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    args = (cnot_10, h1, cgate_z_01, h1)
    expected = (CNOT(i0, i1), H(i0), CGate(i1, Z(i0)), H(i0))
    exp_map = {i0: Integer(1), i1: Integer(0)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    i2 = Symbol('i2')
    ccgate_z = CGate(0, CGate(1, Z(2)))
    ccgate_x = CGate(1, CGate(2, X(0)))
    args = (ccgate_z, ccgate_x)

    expected = (CGate(i0, CGate(i1, Z(i2))), CGate(i1, CGate(i2, X(i0))))
    exp_map = {i0: Integer(0), i1: Integer(1), i2: Integer(2)}
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map

    ndx_map = {i0: Integer(0)}
    index_gen = numbered_symbols(prefix='i', start=1)
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args,
                                                             qubit_map=ndx_map,
                                                             start=i0,
                                                             gen=index_gen)
    assert actual == expected
    assert act_map == exp_map

    i3 = Symbol('i3')
    cgate_x0_c321 = CGate((3, 2, 1), X(0))
    exp_map = {i0: Integer(3), i1: Integer(2), i2: Integer(1), i3: Integer(0)}
    expected = (CGate((i0, i1, i2), X(i3)), )
    args = (cgate_x0_c321, )
    actual, act_map, sndx, gen = convert_to_symbolic_indices(args)
    assert actual == expected
    assert act_map == exp_map
Esempio n. 12
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,)) == set([((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
Esempio n. 13
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')]]))",
    )
Esempio n. 14
0
def test_sympy__physics__quantum__gate__CGate():
    from sympy.physics.quantum.gate import CGate, Gate
    assert _test_args(CGate((0, 1), Gate(2)))