Ejemplo n.º 1
0
def test_cancel() -> None:
    b = AerBackend()
    tb = TketBackend(b)
    qc = circuit_gen()
    job = execute(qc, tb)
    job.cancel()
    assert job.status() == JobStatus.CANCELLED
Ejemplo n.º 2
0
def test_samples() -> None:
    qc = circuit_gen(True)
    b = AerBackend()
    for comp in (None, b.default_compilation_pass()):
        tb = TketBackend(b, comp)
        job = execute(qc, tb, shots=100, memory=True)
        shots = job.result().get_memory()
        assert all(((r[0] == "1" and r[1] == r[2]) for r in shots))
        counts = job.result().get_counts()
        assert all(((r[0] == "1" and r[1] == r[2]) for r in counts.keys()))
Ejemplo n.º 3
0
def test_unitary() -> None:
    qc = circuit_gen()
    b = AerUnitaryBackend()
    for comp in (None, b.default_compilation_pass()):
        tb = TketBackend(b, comp)
        job = execute(qc, tb)
        u = job.result().get_unitary()
        qb = Aer.get_backend("unitary_simulator")
        job2 = execute(qc, qb)
        u2 = job2.result().get_unitary()
        assert np.allclose(u, u2)
Ejemplo n.º 4
0
def test_state() -> None:
    qc = circuit_gen()
    b = AerStateBackend()
    for comp in (None, b.default_compilation_pass()):
        tb = TketBackend(b, comp)
        assert QuantumInstance(tb).is_statevector
        job = execute(qc, tb)
        state = job.result().get_statevector()
        qb = Aer.get_backend("statevector_simulator")
        job2 = execute(qc, qb)
        state2 = job2.result().get_statevector()
        assert np.allclose(state, state2)
Ejemplo n.º 5
0
def test_aqua_algorithm() -> None:
    backends: List[Backend] = [AerBackend(), AerStateBackend()]
    if use_qulacs:
        backends.append(QulacsBackend())
    for b in backends:
        for comp in (None, b.default_compilation_pass()):
            if use_qulacs and type(b) == QulacsBackend and comp is None:
                continue
            tb = TketBackend(b, comp)
            ora = TruthTableOracle(bitmaps="01100110")
            alg = BernsteinVazirani(oracle=ora, quantum_instance=tb)
            result = alg.run()
            assert result["result"] == "011"
            alg = DeutschJozsa(oracle=ora, quantum_instance=tb)
            result = alg.run()
            assert result["result"] == "balanced"
            ora = TruthTableOracle(bitmaps="11111111")
            alg = DeutschJozsa(oracle=ora, quantum_instance=tb)
            result = alg.run()
            assert result["result"] == "constant"
Ejemplo n.º 6
0
print("fval={}".format(results.fval))

exact_solver = MinimumEigenOptimizer(NumPyMinimumEigensolver())
exact_result = exact_solver.solve(qp)
print("x={}".format(exact_result.x))
print("fval={}".format(exact_result.fval))

# Since the `pytket` extension modules provide an interface to the widest variety of devices and simulators out of all major quantum software platforms, the simplest advantage to obtain through `pytket` is to try using some alternative backends.
#
# One such backend that becomes available is the Qulacs simulator, providing fast noiseless simulations, especially when exploiting an available GPU. We can wrap up the `QulacsBackend` (or `QulacsGPUBackend` if you have a GPU available) in a form that can be passed to any Qiskit Aqua algorithm.

from pytket.extensions.qulacs import QulacsBackend
from pytket.extensions.qiskit.tket_backend import TketBackend

qulacs = QulacsBackend()
backend = TketBackend(qulacs, qulacs.default_compilation_pass())

grover_optimizer = GroverOptimizer(6, num_iterations=10, quantum_instance=backend)
results = grover_optimizer.solve(qp)
print("x={}".format(results.x))
print("fval={}".format(results.fval))

# Adding extra backends to target is nice, but where `pytket` really shines is in its compiler passes. The ability to exploit a large number of sources of redundancy in the circuit structure to reduce the execution cost on a noisy device is paramount in the NISQ era. We can examine the effects of this by looking at how effectively the algorithm works on the `qasm_simulator` from Qiskit Aer with a given noise model.
#
# (Note: some versions of `qiskit-aqua` give an `AssertionError` when the `solve()` step below is run. If you encounter this, try updating `qiskit-aqua` or, as a workaround, reducing the number of iterations to 2.)

from qiskit.providers.aer import Aer
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error
from qiskit.aqua import QuantumInstance

backend = Aer.get_backend("qasm_simulator")