Download

Download this file as Jupyter notebook: qiskit_submission_example.ipynb.

Example: Running Jobs on Qiskit Backends

True-Q™ supports direct execution of circuit collections on hardware and simulators that use a Qiskit backend. This includes the IBMQ superconducting qubit chips . Transpiling to the given backend is done automatically for any backend whose gates are a subset of ['id', 'rz', 'sx', 'x', 'cx'] 1.

Qiskit backends accept job requests that contain one or more circuits to be run at a specified number of shots. Like many modern quantum hardware platforms, these backends have restrictions on how many circuits a user can submit per job, and jobs are prioritized in a queuing system that contains many users’ jobs. Thus, large circuits collections (e.g. as generated by True-Q™ diagnostic protocols) must be batched into multiple jobs.

The True-Q™ Executor class automates the batching and job submission process according to the above restrictions. An asynchronous thread monitors the status of each submitted job, and submits new jobs as spots open in the queue.

[2]:
import trueq as tq
import qiskit as qk

Running jobs on a Qiskit backend requires credentials for the provider of the backend. See the provider’s documentation for instructions for how to set this up. For example, the following snippet demonstrates how one instantiates a remote backend object from the IBM Quantum Experience. This example file does not have any credentials, so we make do with the local Qiskit simulator which uses the same backend abstraction.

[3]:
have_credentials = False

if have_credentials:
    qk.IBMQ.load_account()
    provider = qk.IBMQ.get_provider()
    backend = provider.get_backend("ibmq_manila")
else:
    from qiskit.providers.aer import AerSimulator
    from qiskit.test.mock import FakeMelbourne

    backend = AerSimulator.from_backend(FakeMelbourne())
Warning: The module 'qiskit.test.mock' is deprecated since Qiskit Terra 0.21.0, and will be removed 3 months or more later. Instead, you should import the desired object directly 'qiskit.providers.fake_provider'.
         (/tmp/ipykernel_3842806/1456610236.py:9)

Submitting a Circuit

The Executor accepts both single Circuits and CircuitCollections. For example:

[4]:
# Define a simple 2-qubit circuit to work with.
circuit = tq.Circuit([{0: tq.Gate.h}, {(0, 1): tq.Gate.cx}])
circuit.measure_all()

ex = tq.interface.Executor(circuit, backend, n_shots=128)

# the executor is asynchronous, call a blocking function to wait for it to finish
ex.block()
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".

The Executor automatically populates the Results attribute of the submitted circuit:

[5]:
print(circuit.results)
circuit.results.plot()
Results({'11': 64, '00': 60, '01': 3, '10': 1})
../../_images/guides_run_qiskit_submission_example_8_1.png

Submitting a Circuit Collection

To submit a CircuitCollection we can make use of the batch() method (see also Example: Circuit Batching), as the following example demonstrates:

[6]:
# Define a 3-qubit cycle to work with.
cycle = {0: tq.Gate.x, 1: tq.Gate.y, 2: tq.Gate.h}

# Generate a circuit collection to measure noise.
circuits = tq.make_knr(cycle, [4, 32, 64], 24)

The executor (defined below) will automatically attempt to batch the circuit collection into the maximum number of circuits per job that the backend supports. Here, however, we manually batch beforehand. Supposing the backend accepts at most 75 circuits and has a memory cutoff for the number of gates allowed per job, we choose to riffle circuits in the batch by circuit depth. In our protocol above, we selected 3 sequence lengths, 4, 32, and 64, with 24 random circuits per sequence length per configuration. Thus we use fit \(24\times 3+2=72\) circuits per batch, where the extra \(2\) are readout calibration (RCAL) circuits.

[7]:
ro_circuits = tq.make_rcal(circuits.labels)
batches = circuits.batch(74, extra_circuits=ro_circuits, sequencer=tq.sequencer.RIFFLER)

Run the batches on our backend. If a filename is provided, it will periodically save to the given file so that we can resume the experiment if, for example, our Python kernel crashes.

[8]:
ex = tq.interface.Executor(batches, backend, n_shots=128)

# the executor is asynchronous, call a blocking function to wait for it to finish
ex.block()

circuits.plot.timestamps()
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
../../_images/guides_run_qiskit_submission_example_14_6.png

Note

When running in Jupyter, the executor has an automatically updating output which relies on IPywidgets being installed and enabled. If these are not installed then no display will show up when running the executor in Jupyter.

Transpiling for a Specific Backend

Sometimes it is useful to see what the circuit conversion is doing for a particular circuit. To do this, we first instantiate a True-Q configuration object from our desired backend. This will contain the device topology and native gates of the backend. We create a compiler object based on this configuration.

Note

This process is done during submission automatically by the Executor, and the steps below should only be used as a reference. The output of this should not be put into the Executor or it will apply the same compiler operations a second time, which may alter the circuit further.

[9]:
config = tq.interface.qiskit.config_from_backend(backend)
transpiler = tq.Compiler.from_config(config)

Define a circuit.

[10]:
circuit = tq.Circuit([{4: tq.Gate.random(2), 5: tq.Gate.x}])
circuit
[10]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(4): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 148.902
  • 'X': 72.188
  • 'Z': 31.667
Matrix:
  • 0.20 0.08j 0.32 -0.92j 0.52 0.82j -0.17 0.12j
(5): Gate.x
Name:
  • Gate.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00

Transpile the circuit based on the device.

[11]:
transpiled_circuit = transpiler.compile(circuit)
transpiled_circuit
[11]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(4): U3Gate(theta, phi, ...)
Name:
  • U3Gate(theta, phi, ...)
Parameters:
  • theta = 2.713867
  • phi = 0.626855
  • lam = 1.529682
Generators:
  • 'Y': 148.902
  • 'X': 72.188
  • 'Z': 31.667
Matrix:
  • 0.10 -0.19j -0.88 -0.43j 0.88 -0.43j 0.10 0.19j
(5): U3Gate(theta, phi, ...)
Name:
  • U3Gate(theta, phi, ...)
Aliases:
  • Gate.x
  • Gate.cliff1
Parameters:
  • theta = 3.141593
  • phi = -3.141593
  • lam = 0.0
Generators:
  • 'X': 180.0
Matrix:
  • -1.00j -1.00j

Footnotes

1

For older IBM devices, we also support the gateset ['u1', 'u2', 'u3', 'cx'].


Download

Download this file as Jupyter notebook: qiskit_submission_example.ipynb.