Ejemplo n.º 1
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
Ejemplo n.º 2
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))

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

    assert result_1['0'] > 450
    assert result_1['1'] > 450
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
0
def test_crz_integration():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(CRZ(0, 1, angle=math.pi / 3))

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

    assert result['11'] == 0
    assert result['00'] == 1024
    assert result['10'] == 0
    assert result['01'] == 0
Ejemplo n.º 9
0
def test_ch_integration():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(PauliX(0))
    circuit.add(CH(0, 1))

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

    assert result['11'] > 450
    assert result['00'] == 0
    assert result['10'] > 450
    assert result['01'] == 0
Ejemplo n.º 10
0
def test_single_qubit():
    circuit = Circuit()
    circuit.add(Qubits(1))
    circuit.add(Hadamard())  # Can also use H()
    circuit.add(Measure())

    from shor.backends import QuantumSimulator, QSession

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

    # Accounting for random noise, results won't be exact
    assert result[bin(0)] > 450
    assert result[bin(1)] > 450
Ejemplo n.º 11
0
def test_entanglement():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(Hadamard(0))
    circuit.add(CNOT(0, 1))
    circuit.add(Measure(0, 1))

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

    assert result['01'] == 0
    assert result['10'] == 0
    assert result['00'] > 450
    assert result['11'] > 450
Ejemplo n.º 12
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))

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

    assert result['11'] == 0
    assert result['00'] == 0
    assert result['10'] == 0
    assert result['01'] == 1024
Ejemplo n.º 13
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))

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

    # All 16 states should be relatively equal probability
    assert len(result.counts) == 16
    assert max(result.counts.values()) - min(result.counts.values()) < 50
Ejemplo n.º 14
0
def test_Cz_int():
    circuit = Circuit()
    circuit.add(Qubits(2))
    circuit.add(Hadamard(0))
    circuit.add(Hadamard(1))
    circuit.add(Cz(0, 1))
    circuit.add(Measure(0, 1))

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

    assert result['00'] > 210
    assert result['01'] > 210
    assert result['10'] > 210
    assert result['11'] > 210
Ejemplo n.º 15
0
Archivo: shor.py Proyecto: jywyq/shor
def find_period(a, N):
    """ WIP: Quantum subroutine for shor's algorithm.
    Finds the period of a function of the form:
    f(x) = a^x % N

    This uses the quantum fourier transform.
    """

    circuit = Circuit()
    circuit.add(Qubits(5))
    circuit.add(QFT(0, 1, 2, 3))
    circuit.add(quantum_amod_15(a))
    circuit.add(QFT(3, 2, 1, 0))  # Inverse Quantum Fourier transform

    from shor.backends import QuantumSimulator, QSession

    sess = QSession(backend=QuantumSimulator())
    result = sess.run(circuit, num_shots=1024)
    return result
Ejemplo n.º 16
0
def test_ccnot_integration():
    circuit = Circuit()
    circuit.add(Qubits(3))
    circuit.add(PauliX(0))
    circuit.add(PauliX(1))
    circuit.add(CCNOT(0, 1, 2))
    circuit.add(Measure(0, 1, 2))

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

    assert result['000'] == 0
    assert result['001'] == 0
    assert result['010'] == 0
    assert result['100'] == 0
    assert result['110'] == 0
    assert result['101'] == 0
    assert result['011'] == 0
    assert result['111'] == 1024
Ejemplo n.º 17
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))

    sess = QSession(backend=QuantumSimulator())
    result_1 = sess.run(symmetric_circuit_1, num_shots=1024)
    result_2 = sess.run(symmetric_circuit_2, num_shots=1024)

    assert result_1.counts.get(0) == 1024
    assert result_2.counts.get(3) == 1024
Ejemplo n.º 18
0
def test_qsession_run():
    from shor.backends import QSession
    sess = QSession()

    from shor.quantum import Circuit
    sess.run(Circuit().add(Qbits(1)).add(H()), num_shots=10)