Note
Click here to download the full example code
Running Jobs on Qiskit Backends¶
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.
have_credentials = False
if have_credentials:
qk.IBMQ.load_account()
provider = qk.IBMQ.get_provider()
# request the 5-qubit chip called "ibmqx2"
backend = provider.get_backend("ibmqx2")
else:
from qiskit.test.mock import FakeMelbourne
backend = FakeMelbourne()
Submitting a Circuit Collection¶
# 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.
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.
ex = tq.interface.qiskit.Executor(batches, backend, n_shots=128)
# the executor is asynchronous, call a blocking function to wait for it to finish
ex.results()
circuits.plot.timestamps()

Out:
HTML(value="<p style='line-height: 0.1'>Batch    1 of    3. Status: Waiting to start...</p>")
HTML(value="<p style='line-height: 0.1'>Batch    2 of    3. Status: Waiting to start...</p>")
HTML(value="<p style='line-height: 0.1'>Batch    3 of    3. Status: Waiting to start...</p>")
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.
config = tq.interface.qiskit.config_from_backend(backend)
transpiler = tq.compilation.get_transpiler(config)
Define a circuit.
circuit = tq.Circuit([{4: tq.Gate.random(2), 5: tq.Gate.x}])
circuit
Transpile the circuit based on the device.
transpiled_circuit = transpiler.compile(circuit)
transpiled_circuit
Total running time of the script: ( 0 minutes 23.201 seconds)