Beispiel #1
0
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(X(4))
    # circuit.add(quantum_amod_15(a))
    # circuit.add(QFT(0, 1, 2, 3))

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

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

    # TODO: This is still broken, likely due to differences between Qiskit and our library
    # See: https://github.com/shor-team/shor/issues/39
    # from shor.utils.visual import plot_results
    # plot_results(result)

    most_common = result.counts.most_common()
    if len(most_common) > 1:
        return gcd(most_common[0][0], most_common[1][0])
    return 1
Beispiel #2
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])
Beispiel #3
0
def test_hadamard_init():
    from shor.gates import Hadamard, H
    gate1 = H()
    gate2 = Hadamard()

    assert gate1.__class__ == gate2.__class__

    # Try with parameter
    H(0)
Beispiel #4
0
def qft(qubits: List[int]) -> Circuit:
    qc = Circuit()
    for i in range(len(qubits)):
        for k in range(i):
            qc.add(Rx(qubits[i], qubits[k], angle=math.pi/float(2**(i-k))))
        qc.add(H(qubits[i]))
    return qc
Beispiel #5
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
def test_QFT():
    qbits = Qbits(4)
    X = Circuit()
    X.add(qbits)
    X.add(H(0)).add(H(1)).add(H(2)).add(H(3))
    X.add(QFT(0, 1, 2, 3))
    X.add(Measure([0, 1, 2, 3]))
    #
    # qc2 = QuantumCircuit() + H(qbits[0]) + X(qbits[1])
    #
    # qbits = Qbits(4)
    # qc = H(qbits) * QFT(qbits)
    #
    # qc += QFT(qbits[0:3])
    # qc += qc2
    #
    # qc.add(H())
    #
    # qc = H(qc[0:3])
    # qc = Z(qc[1])
    #
    # qbits = Qbits(4)
    # cbits = Cbits(4)
    #
    # qc = QuantumCircuit(qbits=qbits, cbits=cbits)
    # -> err
    # qc += Hadamard(qbits[0]) + Z(qbits[1])
    #
    # qc.add(Measure([qbits[0]]))
    # qc += Measure(qbits[0])
    #
    # qc.add(Measure(qbits), name='Output')

    # X = Circuit(qbits)
    # X = H(X)
    # Y = Z(X)
    # A = H(X)
    #
    # qbits = Qbits(4)
    # X = Circuit() + qbits + H(qbits) + QFT(qbits) + Measure(qbits)
    #
    # Y = X.run(QuantumSimulator, times=100)
    job = X.run(1024)
    result = job.result
    assert result
Beispiel #7
0
def test_hadamard_matrix():
    from shor.gates import H, Hadamard

    gates = [Hadamard(), H()]

    for g in gates:
        assert is_square(g.to_matrix())
        assert is_unitary(g.to_matrix())
        assert np.array_equal(g.to_matrix(), np.multiply(np.divide(1, np.sqrt(2)), np.array([[1, 1], [1, -1]])))
Beispiel #8
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]
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
Beispiel #10
0
def test_simons():
    qbits = Qbits(6)

    # Q = QuantumCircuit(qbits)  # Doesn't work, not implemented
    Q = QuantumCircuit().add(qbits)
    Q.add(H(0)).add(H(1)).add(H(2))
    Q.add(CNOT(0, 3)).add(CNOT(1, 4)).add(CNOT(2, 5))
    Q.add(CNOT(1, 4)).add(CNOT(1, 5))
    Q.add(H(0)).add(H(1)).add(H(2))
    Q.add(Measure(qbits[:3]))  # Doesn't work, partial measurements...

    job = Q.run(1000, provider=QiskitProvider())
    result = job.result

    print(result)
def test_quantum_multi_hadamard_partial_measure():
    circuit_1 = Circuit()
    circuit_1.add(Qubits(4))
    circuit_1.add(H(range(4)))
    circuit_1.add(Measure(0, 1))

    result_1 = circuit_1.run(1024).result

    print(result_1.counts)

    assert all(r > 215 for r in [
        result_1["0000"], result_1["0001"], result_1["0010"], result_1["0011"]
    ])

    # Ony measuring the 2 right-most qbits. Shouldn't see measurements for other qbits.
    assert result_1["1000"] == result_1["1001"] == result_1[
        "1010"] == result_1["1011"] == 0
    assert result_1["1100"] == result_1["1101"] == result_1[
        "1110"] == result_1["1111"] == 0
    assert result_1["0100"] == result_1["0101"] == result_1[
        "0110"] == result_1["0111"] == 0
Beispiel #12
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)