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:
    backend = qk.providers.aer.QasmSimulator()

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()
Results Timestamps

Out:

HTML(value="<p style='line-height: 0.1'>Batch &nbsp&nbsp&nbsp1 of &nbsp&nbsp&nbsp3.  Status: Waiting to start...</p>")
HTML(value="<p style='line-height: 0.1'>Batch &nbsp&nbsp&nbsp2 of &nbsp&nbsp&nbsp3.  Status: Waiting to start...</p>")
HTML(value="<p style='line-height: 0.1'>Batch &nbsp&nbsp&nbsp3 of &nbsp&nbsp&nbsp3.  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
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.
   
 
(4): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 9.288
  • 'Y': -97.255
  • 'Z': -62.364
Matrix:
  • 0.11 -0.69j -0.43 -0.57j 0.32 0.64j -0.67 -0.21j
(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.

transpiled_circuit = transpiler.compile(circuit)
transpiled_circuit
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.
   
 
(4): qasm_simulator.u3(theta, phi, ...)
Name:
  • qasm_simulator.u3
Parameters:
  • theta = 1.591689
  • phi = -3.756368
  • lam = 2.336399
Generators:
  • 'X': 9.288
  • 'Y': -97.255
  • 'Z': -62.364
Matrix:
  • 0.60 0.36j 0.69 -0.19j -0.71 0.05j 0.44 -0.54j
(5): qelib1.x()
Name:
  • qelib1.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00


Total running time of the script: ( 0 minutes 15.119 seconds)

Gallery generated by Sphinx-Gallery