Пример #1
0
def test_teleportation():
    circuit = Circuit()
    circuit.add(Qubits(3))
    circuit.add(CNOT(1, 2))
    circuit.add(CNOT(0, 1))
    circuit.add(Hadamard(0))
    circuit.add(Measure([0, 1]))

    result = circuit.run(1024).result

    # All 16 states should be relatively equal probability
    if result["11"] == 1024:
        circuit.add(PauliX(2))
        circuit.add(PauliZ(2))
    elif result["10"] == 1024:
        circuit.add(PauliZ(2))
    elif result["01"] == 1024:
        circuit.add(PauliX(2))
    circuit.add(Measure([2]))

    result2 = circuit.run(1024).result

    # TODO: Fix this test.

    assert result2
Пример #2
0
def test_cx_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(2))
    circuit_1.add(Hadamard(0))
    circuit_1.add(Cx(0, 1))
    circuit_1.add(Measure(0, 1))

    circuit_2 = Circuit()
    circuit_2.add(Qubits(2))
    circuit_2.add(Hadamard(1))
    circuit_2.add(Cx(0, 1))
    circuit_2.add(Measure(0, 1))

    sess = QSession(backend=QuantumSimulator())
    result_1 = sess.run(circuit_1, num_shots=1024)
    result_2 = sess.run(circuit_2, num_shots=1024)

    assert result_1['01'] == 0
    assert result_1['10'] > 450
    assert result_1['00'] > 450
    assert result_1['11'] == 0

    assert result_2['01'] == 0
    assert result_2['10'] == 0
    assert result_2['00'] > 450
    assert result_2['11'] > 450
Пример #3
0
def simple_circuit():
    qc = QC()
    qc.add(Qbits(3))
    qc.add(H(1))
    qc.add(CNOT(1, 0))
    qc.add(Measure([0, 1]))
    return qc
Пример #4
0
def test_apis():
    qbits = Qbits(4)
    # Qbit
    # id
    # name

    # Implements iterable, so we get all of the nice
    # python indexing functionality
    # even = qbits[::2]  # Get the even indexed qbits
    # odd = qbits[1::2]  # Get the odd indexed qbits

    # TODO: Support multiple qbits, and this test.
    # TODO: Add empty circuit test
    # TODO: Infer qbits used from ciruit rather than from Qbits layer
    # H(even) does not work.

    qc = QuantumCircuit()
    qc += Qbits(1)
    # qc += H(even)
    # qc += X(odd)
    qc += H(0)
    qc.add(Measure([qbits]))

    job = qc.run(100)
    result = job.result

    assert result["1111"] == 0
    # Any single qbit should accept
    # A single qbit
    H(qbits[1])
Пример #5
0
def test_u3_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(1))
    circuit_1.add(U3(0, theta=np.pi / 2, phi=-np.pi / 2, lam=np.pi / 2))
    circuit_1.add(Measure([0]))

    circuit_2 = Circuit()
    circuit_2.add(Qubits(1))
    circuit_2.add(Rx(theta=np.pi))
    circuit_2.add(Measure([0]))
    result_1 = circuit_1.run(1024).result
    result_2 = circuit_2.run(1024).result

    assert result_1["0"] > 450
    assert result_1["1"] > 450
    assert result_2["0"] > 450
    assert result_2["1"] > 450
Пример #6
0
def test_u2_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(1))
    circuit_1.add(U2(0, phi=-np.pi / 2, alpha=np.pi / 2))
    circuit_1.add(Measure([0]))
    result_1 = circuit_1.run(1024).result

    assert result_1["0"] > 450
    assert result_1["1"] > 450
Пример #7
0
def test_t_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(T(0))  # Can also use H()
    circuit.add(Measure([0]))
    job = circuit.run(1024)
    result = job.result
    assert result["0"] == 1024
    assert result["1"] == 0
Пример #8
0
def test_ry_int():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(Ry(0, angle=np.pi / 2))
    circuit.add(Measure([0]))
    job = circuit.run(1000)
    result = job.result
    assert result["0"] > 450
    assert result["1"] > 450
Пример #9
0
def test_inity_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(1))
    circuit_1.add(Init_y(0))
    circuit_1.add(Measure([0]))

    result_1 = circuit_1.run(1024).result

    assert result_1["0"] > 450
    assert result_1["1"] > 450
Пример #10
0
def test_u1_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(PauliX(0))
    circuit.add(U1(0))
    circuit.add(Measure([0]))
    job = circuit.run(1024)
    result = job.result
    assert result["0"] == 0
    assert result["1"] == 1024
Пример #11
0
def test_id_qubit():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(ID(0))
    circuit.add(Measure([0]))
    job = circuit.run(1024)
    result = job.result
    # Accounting for random noise, results won't be exact
    assert result["1"] == 0
    assert result["0"] == 1024
Пример #12
0
def test_pauliy_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(PauliY(0))  # Can also use H()
    circuit.add(Measure([0]))
    job = circuit.run(1024)
    result = job.result
    # Accounting for random noise, results won't be exact
    assert result["0"] == 0
    assert result["1"] == 1024
Пример #13
0
def test_modulus_circuit():
    circuit = Circuit()
    circuit.add(Qbits(5))
    circuit.add(quantum_amod_15(4))
    circuit.add(Measure([0, 1]))

    job = circuit.run(1024)
    result = job.result

    print(result)
Пример #14
0
def test_U3_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(1))
    circuit_1.add(U3(0, theta=np.pi / 2, phi=-np.pi / 2, alpha=np.pi / 2))
    circuit_1.add(Measure(0))

    circuit_2 = Circuit()
    circuit_2.add(Qubits(1))
    circuit_2.add(Rx(theta=np.pi))
    circuit_2.add(Measure(0))

    sess = QSession(backend=QuantumSimulator())
    result_1 = sess.run(circuit_1, num_shots=1024)
    result_2 = sess.run(circuit_2, num_shots=1024)

    assert result_1['0'] > 450
    assert result_1['1'] > 450
    assert result_2['0'] > 450
    assert result_2['1'] > 450
Пример #15
0
def test_s_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(S(0))  # Can also use H()
    circuit.add(Measure())

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1024)

    assert result['0'] == 1024
    assert result['1'] == 0
Пример #16
0
def test_crz_integration():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(CRZ(0, 1, angle=math.pi / 3))
    circuit.add(Measure(0, 1))
    job = circuit.run(1024)
    result = job.result
    assert result["11"] == 0
    assert result["00"] == 1024
    assert result["10"] == 0
    assert result["01"] == 0
Пример #17
0
def test_rz_int():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(Rz(0, angle=np.pi / 2))
    circuit.add(Measure(0))

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1000)

    assert result['0'] == 1000
    assert result['1'] == 0
Пример #18
0
def test_single_qubit():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(Hadamard(0))
    circuit.add(Measure([0]))

    job = circuit.run(1024, provider=QiskitProvider())
    result = job.result
    # Accounting for random noise, results won't be exact
    assert result[bin(0)] > 450
    assert result[bin(1)] > 450
Пример #19
0
def test_crk_int():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(CRk(0, 1, k=2))
    circuit.add(Measure(0, 1))

    result = circuit.run(1000).result

    assert result["00"] == 1000
    assert result["01"] == 0
    assert result["10"] == 0
    assert result["11"] == 0
Пример #20
0
def test_u1_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(PauliX(0))
    circuit.add(U1(0))
    circuit.add(Measure())

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1024)

    assert result['0'] == 0
    assert result['1'] == 1024
Пример #21
0
def test_ID_qubit():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(ID(0))
    circuit.add(Measure())

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1024)

    # Accounting for random noise, results won't be exact
    assert result['1'] == 0
    assert result['0'] == 1024
Пример #22
0
def test_pauliy_integration():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(PauliY(0))  # Can also use H()
    circuit.add(Measure())

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1024)

    # Accounting for random noise, results won't be exact
    assert result['0'] == 0
    assert result['1'] == 1024
Пример #23
0
def test_entanglement():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(Hadamard(0))
    circuit.add(CNOT(0, 1))
    circuit.add(Measure([0, 1]))
    job = circuit.run(1024)
    result = job.result
    assert result["01"] == 0
    assert result["10"] == 0
    assert result["00"] > 450
    assert result["11"] > 450
Пример #24
0
def test_swap_integration():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(PauliX(0))
    circuit.add(SWAP(0, 1))
    circuit.add(Measure([0, 1]))
    job = circuit.run(1024)
    result = job.result
    assert result["11"] == 0
    assert result["00"] == 0
    assert result["01"] == 0
    assert result["10"] == 1024
Пример #25
0
def test_mult_gate_inputs1():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(2))
    circuit_1.add(H([0, 1]))
    circuit_1.add(Measure(0, 1))

    result_1 = circuit_1.run(1024).result

    assert result_1["00"] > 215
    assert result_1["11"] > 215
    assert result_1["10"] > 215
    assert result_1["01"] > 215
Пример #26
0
    def test_circuit_on_simulator(self):
        qc = QC()
        qc.add(Qbits(3))
        qc.add(H(1))
        qc.add(CNOT(1, 0))
        qc.add(Measure([0, 1]))

        ibm_provider = IBMQ()
        job = qc.run(1024, ibm_provider)
        result = job.result
        counts = result.counts
        assert counts[0] > 450 < counts[3]
Пример #27
0
def test_cr_int():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(Cr(0, 1, angle=np.pi / 2))
    circuit.add(Measure(0, 1))

    result = circuit.run(1000).result

    assert result["00"] == 1000
    assert result["01"] == 0
    assert result["10"] == 0
    assert result["11"] == 0
Пример #28
0
def test_multi_hadamard():
    circuit = Circuit()
    circuit.add(Qubits(4))
    circuit.add(Hadamard(0))
    circuit.add(Hadamard(1))
    circuit.add(Hadamard(2))
    circuit.add(Hadamard(3))
    circuit.add(Measure([0, 1, 2, 3]))
    job = circuit.run(1024)
    result = job.result
    # All 16 states should be relatively equal probability
    assert len(result.counts) == 16
    assert max(result.counts.values()) - min(result.counts.values()) < 50
Пример #29
0
def test_cz2_int():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(2))
    circuit_1.add(PauliX(0))
    circuit_1.add(Cz(0, 1))
    circuit_1.add(Measure(0, 1))

    result_1 = circuit_1.run(1024).result

    assert result_1["00"] == 0
    assert result_1["11"] == 0
    assert result_1["01"] == 1024
    assert result_1["10"] == 0
Пример #30
0
def test_unitary_symmetry_does_nothing():
    symmetric_circuit_1 = Circuit()
    symmetric_circuit_1.add(Qubits(2))
    symmetric_circuit_1.add(Hadamard(0))
    symmetric_circuit_1.add(Hadamard(0))
    symmetric_circuit_1.add(CNOT(0, 1))
    symmetric_circuit_1.add(CNOT(0, 1))
    symmetric_circuit_1.add(Measure([0, 1]))

    symmetric_circuit_2 = Circuit()
    symmetric_circuit_2.add(Qubits(2, state=1))
    symmetric_circuit_2.add(Hadamard(0))
    symmetric_circuit_2.add(CNOT(0, 1))
    symmetric_circuit_2.add(CNOT(0, 1))
    symmetric_circuit_2.add(Hadamard(0))
    symmetric_circuit_2.add(Measure([0, 1]))

    result_1 = symmetric_circuit_1.run(1024).result
    result_2 = symmetric_circuit_2.run(1024).result

    assert result_1.counts.get(0) == 1024
    assert result_2.counts.get(0) == 1024