Example #1
0
def run_bv_circuit(eng, input_size, s_int):
    """Run the quantum circuit."""
    s = ('{0:0' + str(input_size) + 'b}').format(s_int)
    print("Secret string: ", s)
    print("Number of qubits: ", str(input_size + 1))
    circuit = eng.allocate_qureg(input_size + 1)
    All(H) | circuit
    Z | circuit[input_size]

    Barrier | circuit

    oracle(circuit, input_size, s)

    Barrier | circuit

    qubits = circuit[:input_size]
    All(H) | qubits
    All(Measure) | qubits
    eng.flush()

    # return a random answer from our results
    histogram(eng.backend, qubits)
    plt.show()

    # return a random answer from our results
    probabilities = eng.backend.get_probabilities(qubits)
    random_answer = random.choice(list(probabilities.keys()))
    print("Probability of getting correct string: ", probabilities[s[::-1]])
    return [int(s) for s in random_answer]
Example #2
0
def run_entangle(eng, num_qubits=3):
    """
    Run an entangling operation on the provided compiler engine.

    Args:
        eng (MainEngine): Main compiler engine to use.
        num_qubits (int): Number of qubits to entangle.

    Returns:
        measurement (list<int>): List of measurement outcomes.
    """
    # allocate the quantum register to entangle
    qureg = eng.allocate_qureg(num_qubits)

    # entangle the qureg
    Entangle | qureg

    # measure; should be all-0 or all-1
    All(Measure) | qureg

    # run the circuit
    eng.flush()

    # access the probabilities via the back-end:
    # results = eng.backend.get_probabilities(qureg)
    # for state in results:
    #     print("Measured {} with p = {}.".format(state, results[state]))
    # or plot them directly:
    histogram(eng.backend, qureg)
    plt.show()

    # return one (random) measurement outcome.
    return [int(q) for q in qureg]
def test_invalid_backend(matplotlib_setup):
    eng = MainEngine(backend=DummyEngine())
    qubit = eng.allocate_qubit()
    eng.flush()

    with pytest.raises(RuntimeError):
        histogram(eng.backend, qubit)
Example #4
0
def test_qureg(matplotlib_setup):
    sim = Simulator()
    eng = MainEngine(sim)
    qureg = eng.allocate_qureg(3)
    eng.flush()
    _, _, prob = histogram(sim, qureg)
    assert prob["000"] == pytest.approx(1)
    assert prob["110"] == pytest.approx(0)
    H | qureg[0]
    C(X, 1) | (qureg[0], qureg[1])
    H | qureg[2]
    eng.flush()
    _, _, prob = histogram(sim, qureg)
    assert prob["110"] == pytest.approx(0.25)
    assert prob["100"] == pytest.approx(0)
    All(Measure) | qureg
    eng.flush()
    _, _, prob = histogram(sim, qureg)
    assert (
        prob["000"] == pytest.approx(1)
        or prob["001"] == pytest.approx(1)
        or prob["110"] == pytest.approx(1)
        or prob["111"] == pytest.approx(1)
    )
    assert prob["000"] + prob["001"] + prob["110"] + prob["111"] == pytest.approx(1)
def circuit(engine):
    q = engine.allocate_qureg(2)
    H | q[0]
    CX | (q[0], q[1])
    #All(Measure) | q

    engine.flush()
    histogram(eng.backend, q)
    plt.show()
def circuit(engine):
    q = engine.allocate_qureg(2)

    q = oracale(q)
    q = reflection(q)

    #All(Measure) | q

    engine.flush()

    histogram(eng.backend, q)
    plt.show()
def test_qubit(matplotlib_setup):
    sim = Simulator()
    eng = MainEngine(sim)
    qubit = eng.allocate_qubit()
    eng.flush()
    _, _, prob = histogram(sim, qubit)
    assert prob["0"] == pytest.approx(1)
    assert prob["1"] == pytest.approx(0)
    H | qubit
    eng.flush()
    _, _, prob = histogram(sim, qubit)
    assert prob["0"] == pytest.approx(0.5)
    Measure | qubit
    eng.flush()
    _, _, prob = histogram(sim, qubit)
    assert prob["0"] == pytest.approx(1) or prob["1"] == pytest.approx(1)
def test_combination(matplotlib_setup):
    sim = Simulator()
    eng = MainEngine(sim)
    qureg = eng.allocate_qureg(2)
    qubit = eng.allocate_qubit()
    eng.flush()
    _, _, prob = histogram(sim, [qureg, qubit])
    assert prob["000"] == pytest.approx(1)
    H | qureg[0]
    C(X, 1) | (qureg[0], qureg[1])
    H | qubit
    Measure | qureg[0]
    eng.flush()
    _, _, prob = histogram(sim, [qureg, qubit])
    assert (prob["000"] == pytest.approx(0.5) and prob["001"] == pytest.approx(0.5)) \
        or (prob["110"] == pytest.approx(0.5) and prob["111"] == pytest.approx(0.5))
    assert prob["100"] == pytest.approx(0)
    Measure | qubit
def test_too_many_qubits(matplotlib_setup, capsys):
    sim = Simulator()
    eng = MainEngine(sim)
    qureg = eng.allocate_qureg(6)
    eng.flush()
    l_ref = len(capsys.readouterr().out)
    _, _, prob = histogram(sim, qureg)
    assert len(capsys.readouterr().out) > l_ref
    assert prob["000000"] == pytest.approx(1)
    All(Measure)
Example #10
0
def run_half_adder(eng):
    """Run the half-adder circuit."""
    # allocate the quantum register to entangle
    circuit = eng.allocate_qureg(4)
    qubit1, qubit2, qubit3, qubit4 = circuit
    result_qubits = [qubit3, qubit4]

    # X gates on the first two qubits
    All(X) | [qubit1, qubit2]

    # Barrier
    Barrier | circuit

    # Cx gates
    CNOT | (qubit1, qubit3)
    CNOT | (qubit2, qubit3)

    # CCNOT
    Toffoli | (qubit1, qubit2, qubit4)

    # Barrier
    Barrier | circuit

    # Measure result qubits
    All(Measure) | result_qubits

    # Flush the circuit (this submits a job to the IonQ API)
    eng.flush()

    # Show the histogram
    histogram(eng.backend, result_qubits)
    plt.show()

    # return a random answer from our results
    probabilities = eng.backend.get_probabilities(result_qubits)
    random_answer = random.choice(list(probabilities.keys()))
    return [int(s) for s in random_answer]
def test_backend_get_probabilities_method(matplotlib_setup):
    class MyBackend(BasicEngine):
        def get_probabilities(self, qureg):
            return {'000': 0.5, '111': 0.5}

        def is_available(self, cmd):
            return True

        def receive(self, command_list):
            for cmd in command_list:
                if not isinstance(cmd.gate, FlushGate):
                    assert isinstance(cmd.gate, AllocateQubitGate)

    eng = MainEngine(backend=MyBackend(), verbose=True)
    qureg = eng.allocate_qureg(3)
    eng.flush()
    _, _, prob = histogram(eng.backend, qureg)
    assert prob['000'] == 0.5
    assert prob['111'] == 0.5
Example #12
0
# -*- coding: utf-8 -*-
# pylint: skip-file
"""Example implementation of a quantum circuit generating a Bell pair state."""

import matplotlib.pyplot as plt
from teleport import create_bell_pair

from projectq import MainEngine
from projectq.backends import CircuitDrawer
from projectq.libs.hist import histogram
from projectq.setups.default import get_engine_list

# create a main compiler engine
drawing_engine = CircuitDrawer()
eng = MainEngine(engine_list=get_engine_list() + [drawing_engine])

qb0, qb1 = create_bell_pair(eng)

eng.flush()
print(drawing_engine.get_latex())

histogram(eng.backend, [qb0, qb1])
plt.show()