示例#1
0
def main():
    qc_a = QuantumComputer(2)
    qc_a.X(0)
    qc_a.CNOT(0, 1)
    print("2 (A): ", qc_a.ket_notation())

    qc_b = QuantumComputer(2)
    qc_b.H(0)
    qc_b.CNOT(0, 1)
    print("2 (B): ", qc_b.ket_notation())
def question4():
    state = 1 / np.sqrt(2) * (np.kron(ZERO, ZERO) - np.kron(ONE, ONE))
    ix = QuantumComputer(state).I(0).X(1).ket_notation()
    iy = QuantumComputer(state).I(0).Y(1).ket_notation()
    xy = QuantumComputer(state).X(0).Y(1).ket_notation()

    print()
    print("Question 4: ")
    print(f"IX({to_ket_notation(state)}) = {ix} = |O-⟩")
    print(f"IY({to_ket_notation(state)}) = {iy} = i|O+⟩")
    print(f"XY({to_ket_notation(state)}) = {xy} = i|E+⟩")
def question3():
    qc = QuantumComputer(2)
    qc.H(0)
    qc.H(1)
    probabilities, new_states = zz_parity_measurement(qc.state)
    print()
    print("Question 3: ")
    print("After H gate on each qubit.")
    print(
        f"{to_ket_notation(new_states[0])} with prob = {probabilities[0]:.2f}")
    print(
        f"{to_ket_notation(new_states[1])} with prob = {probabilities[1]:.2f}")
示例#4
0
def question4():
    qc = QuantumComputer(2)
    qc.H(0)
    qc.H(1)
    qc.CNOT(0, 1)
    qc.H(0)
    qc.H(1)

    qc_dash = QuantumComputer(2)
    qc_dash.CNOT(1, 0)

    print("Question 4:", np.isclose(qc.unitary, qc_dash.unitary).all())  # True
示例#5
0
def question5():
    T_GATE = np.array([
        [1, 0],
        [0, np.power(np.e, complex(0, np.pi / 4))]
    ])

    TOFFOLI_GATE = np.array([
        [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]
    ])

    qc = QuantumComputer(3)
    qc.H(2)
    qc.CNOT(1, 2)
    qc.gates(T_GATE.conjugate().transpose(), 2)
    qc.CNOT(0, 2)
    qc.gates(T_GATE, 2)
    qc.CNOT(1, 2)
    qc.gates(T_GATE.conjugate().transpose(), 2)
    qc.CNOT(0, 2)
    qc.gates(T_GATE, 1)
    qc.gates(T_GATE, 2)
    qc.CNOT(0, 1)
    qc.H(2)
    qc.gates(T_GATE, 0)
    qc.gates(T_GATE.conjugate().transpose(), 1)
    qc.CNOT(0, 1)

    print("Question 5:", np.isclose(qc.unitary, TOFFOLI_GATE).all())  # True
示例#6
0
def question3():
    qc = QuantumComputer(2)
    qc.H(1)
    qc.controlled_gate(Z_GATE, 1, 0)
    qc.H(1)
    print("Question 3:", np.isclose(qc.unitary, CNOT_GATE).all())  # True
示例#7
0
def question1():
    qc = QuantumComputer(1)
    qc.H(0)
    qc.Y(0)
    qc.H(0)
    print("Question 1 (XYX = -Y):", np.isclose(qc.unitary, -Y_GATE).all())  # True