示例#1
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])
def QXOR(first_bit, second_bit):
    q0 = zero
    q1 = zero
    if first_bit == 1:
        q0 = np.dot(pauli_x, q0)
    if second_bit == 1:
        q1 = np.dot(pauli_x, q1)
    q_target = zero

    # XOR circuit
    P0 = np.dot(zero, zero.T)
    P1 = np.dot(one, one.T)
    CNOT_on_2 = n_kron(P0, ID2) + n_kron(P1, pauli_x)

    # |q0 q_target>
    q_0_target = n_kron(q0, q_target)
    CNOT_0_target = np.dot(CNOT_on_2, q_0_target)

    _, updated_target = list(
        matrix_to_qubit(CNOT_0_target).free_symbols)[0].qubit_values
    q_target = (lambda x: zero if x == 0 else one)(updated_target)

    # |q1 q_target>
    q_1_target = n_kron(q1, q_target)
    CNOT_1_target = np.dot(CNOT_on_2, q_1_target)

    _, updated_target = list(
        matrix_to_qubit(CNOT_1_target).free_symbols)[0].qubit_values
    q_target = (lambda x: zero if x == 0 else one)(updated_target)

    qubits = measure([a[0] for a in q_target])
    result, = qubits

    return result
示例#3
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])
示例#4
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()
示例#5
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
示例#6
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
示例#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()
示例#8
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
示例#9
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
示例#10
0
def test_superposition_of_states():
    assert qapply(CNOT(0,1)*HadamardGate(0)*(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10'))).expand() == (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 +\
     Qubit('10')/2)

    assert matrix_to_qubit(represent(CNOT(0,1)*HadamardGate(0)\
    *(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10')), nqubits=2))\
     == (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 + Qubit('10')/2)
示例#11
0
def test_superposition_of_states():
    state = 1 / sqrt(2) * Qubit('01') + 1 / sqrt(2) * Qubit('10')
    state_gate = CNOT(0, 1) * HadamardGate(0) * state
    state_expanded = Qubit('01') / 2 + Qubit('00') / 2 - Qubit(
        '11') / 2 + Qubit('10') / 2
    assert qapply(state_gate).expand() == state_expanded
    assert matrix_to_qubit(represent(state_gate, nqubits=2)) == state_expanded
示例#12
0
def test_superposition_of_states():
    assert qapply(CNOT(0, 1)*HadamardGate(0)*(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10'))).expand() == (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 +
     Qubit('10')/2)

    assert matrix_to_qubit(represent(CNOT(0, 1)*HadamardGate(0)
        *(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10')), nqubits=2)) == \
        (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 + Qubit('10')/2)
示例#13
0
def test_superposition_of_states():
    state = 1 / sqrt(2) * Qubit("01") + 1 / sqrt(2) * Qubit("10")
    state_gate = CNOT(0, 1) * HadamardGate(0) * state
    state_expanded = (
        Qubit("01") / 2 + Qubit("00") / 2 - Qubit("11") / 2 + Qubit("10") / 2
    )
    assert qapply(state_gate).expand() == state_expanded
    assert matrix_to_qubit(represent(state_gate, nqubits=2)) == state_expanded
示例#14
0
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)
示例#15
0
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)) ==\
        apply_operators(circuit)
def measure(amplitudes, repetitions=10):
    measurements = []
    for _ in range(repetitions):
        weights   = [abs(amplitude)**2 for amplitude in amplitudes]
        outcome   = random.choices(range(len(amplitudes)), weights)[0]
        new_state = np.zeros((len(amplitudes), 1))
        new_state[outcome][0] = 1
        measurements.append(new_state)
    sample = random.choice(measurements)
    qubit  = list(matrix_to_qubit(np.array(sample)).free_symbols)[0]
    return qubit.qubit_values
示例#17
0
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
示例#18
0
文件: test_gate.py 项目: pyc111/sympy
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")
示例#19
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')
示例#20
0
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
示例#21
0
def fourier_transform():
    psi = n_kron_list([zero]*4)

    print("\nQuantum fourier transform\n")

    psi = apply_hadamard(psi, 0, n=4)

    psi = apply_cz_and_swap(psi, 0, 1,   0.5, n=4)
    psi = apply_cz_and_swap(psi, 1, 2,  0.25, n=4)
    psi = apply_cz_and_swap(psi, 2, 3, 0.125, n=4)

    psi = apply_hadamard(psi, 0, n=4)

    psi = apply_cz_and_swap(psi, 0, 1,  0.5, n=4)
    psi = apply_cz_and_swap(psi, 1, 2, 0.25, n=4)

    psi = apply_hadamard(psi, 0, n=4)

    psi = apply_cz_and_swap(psi, 0, 1,  0.5, n=4)

    psi = apply_hadamard(psi, 0, n=4)


    measurements = []
    repetitions  = 10000
    for idx in range(0, repetitions):
        psi_values = measure([a[0] for a in psi], repetitions=1)
        psi_str    = "".join([str(bit) for bit in psi_values])
        measurements.append(psi_str)
        print(psi_str, "=>", int(psi_str, 2))
        pass

    print("\n", matrix_to_qubit(psi), "\n")

    histogram = Counter(measurements)
    print("\n", list(histogram.keys()), "\n")
    print("\n", histogram, "\n")

    pass
def QOR(first_bit, second_bit):
    q0 = zero
    q1 = zero
    if first_bit == 1:
        q0 = np.dot(pauli_x, q0)
    if second_bit == 1:
        q1 = np.dot(pauli_x, q1)
    q_target = zero

    # OR circuit
    q0 = np.dot(pauli_x, q0)
    q1 = np.dot(pauli_x, q1)
    q_combined = n_kron(q0, q1, q_target)

    new_state = np.dot(toffoli, q_combined)

    _, _, q3 = list(matrix_to_qubit(new_state).free_symbols)[0].qubit_values
    q_target = (lambda x: zero if x == 0 else one)(q3)

    q_target = np.dot(pauli_x, q_target)

    qubits = measure([a[0] for a in q_target])
    result, = qubits
    return result
    qubit_values = measure([a[0] for a in psi])

    _,_,_,_,_,sum_bit,_,carry_out = qubit_values
    return sum_bit, carry_out


if __name__=="__main__":

    # |11001>
    q11001 = n_kron(one, one, zero, zero, one)

    print("\n\nCNOT testing...\n")
    print("CNOT_0_2(|11001>) => |11101>")
    q0 = q11001
    q0 = apply_cnot(q0, 0, 2, 5)
    print(matrix_to_qubit(q0))
    print("CNOT_2_4(|11001>) => |11001>")
    q0 = q11001
    q0 = apply_cnot(q0, 2, 4, 5)
    print(matrix_to_qubit(q0))
    print("CNOT_1_4(|11001>) => |11000>")
    q0 = q11001
    q0 = apply_cnot(q0, 1, 4, 5)
    print(matrix_to_qubit(q0))
    print("CNOT_4_1(|11001>) => |10001>")
    q0 = q11001
    q0 = apply_cnot(q0, 4, 1, 5)
    print(matrix_to_qubit(q0))

    print("\n\nPauli-X testing...\n")
    print("X_0(|11001>) => |01001>")
print("Dagger", q1, Dagger(q1))
ip = Dagger(q1) * q1
print(ip)
print(ip.doit())

print("Qubit-ээс numpy руу хөрвүүлэх")
q = Qubit('01')
print(q)
q_np = np.array(q)
print(q_np)
#q_np0 = np.array(represent(q), dtype=np.cfloat)
q_np0 = np.array(represent(q))
print(q_np0.shape)
print(q_np0)

print("Numpy-аас Qubit-рүү хөрвүүлэх")
# https://stackoverflow.com/questions/30018977/how-can-i-get-a-list-of-the-symbols-in-a-sympy-expression
new_q = matrix_to_qubit(q_np0)
print(new_q.free_symbols)
new_q = matrix_to_qubit(np.array([[1], [0], [1], [0]]))
print(new_q.free_symbols)
new_q = matrix_to_qubit(np.array([[1], [0], [0], [0]]))
print(new_q)
new_q = matrix_to_qubit(np.array([[0], [1], [0], [0]]))
print(new_q)
new_q = matrix_to_qubit(np.array([[0], [0], [1], [0]]))
print(new_q)
new_q = matrix_to_qubit(np.array([[0], [0], [0], [1]]))
print(new_q)
示例#25
0
def test_superposition_of_states():
    state = 1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10')
    state_gate = CNOT(0, 1)*HadamardGate(0)*state
    state_expanded = Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 + Qubit('10')/2
    assert qapply(state_gate).expand() == state_expanded
    assert matrix_to_qubit(represent(state_gate, nqubits=2)) == state_expanded
hadamard = np.array([[1. + 0.j, 1. + 0.j], [1. + 0.j, -1. + 0.j]
                     ]) * 1 / np.sqrt(2)

ID = np.eye(2, dtype=np.cfloat)

swap = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]],
                dtype=np.cfloat)

# |0> болон |1> төлвүүдээр цэнэглэгдсэн qubit-үүд
q0 = zero
q1 = one

x_flipped_q0 = np.dot(pauli_x, q0)
print("Pauli-X(|0>)")
print(matrix_to_qubit(x_flipped_q0), "\n")

x_flipped_q1 = np.dot(pauli_x, q1)
print("Pauli-X(|1>)")
print(matrix_to_qubit(x_flipped_q1), "\n")

y_flipped_q0 = np.dot(pauli_y, q0)
print("Pauli-Y(|0>)")
print(matrix_to_qubit(y_flipped_q0), "\n")

y_flipped_q1 = np.dot(pauli_y, q1)
print("Pauli-Y(|1>)")
print(matrix_to_qubit(y_flipped_q1), "\n")

z_flipped_q0 = np.dot(pauli_z, q0)
print("Pauli-Z(|0>)")