# 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:
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()


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

 Circuit Key: No key present in circuit. (4): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 106.216 'Y': 83.538 'Z': -10.471 Matrix: 0.37 0.09j -0.54 -0.75j 0.60 -0.70j 0.38 -0.05j (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

 Circuit Key: No key present in circuit. (4): qasm_simulator.U3Gate(theta, phi, ...) Name: qasm_simulator.U3Gate Parameters: theta = 2.351062 phi = -1.091143 lam = 0.717544 Generators: 'X': 106.216 'Y': 83.538 'Z': -10.471 Matrix: 0.39 -0.70 -0.61j 0.43 -0.82j 0.36 -0.14j (5): qasm_simulator.U3Gate(theta, phi, ...) Name: qasm_simulator.U3Gate Aliases: Gate.x Gate.cliff1 Parameters: theta = 3.141593 phi = -1.570796 lam = 1.570796 Generators: 'X': 180.0 Matrix: -1.00j -1.00j

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

Gallery generated by Sphinx-Gallery