%pip install qiskit
%pip install tergite
Equal Superposition
This is a showcase of connecting to tergite via the tergite
SDK, running a basic one-qubit circuit to produce the equal superposition state \(|\Psi\rangle = |0\rangle + |1\rangle\), and retrieving the measurement results.
Install dependencies
This example depends on:
Install these dependencies
Import the basic dependencies
import time
import qiskit.circuit as circuit
import qiskit.compiler as compiler
from tergite.qiskit.providers import Tergite
from tergite.qiskit.providers.provider_account import ProviderAccount
Configure Session
Before we get any further, we will take the time to define some of the parameters we will use for our tergite job.
# the Tergite API URL e.g. "https://api.tergite.example"
= "https://api.qal9000.se"
API_URL # API token for connecting to tergite
= "API-TOKEN"
API_TOKEN # The name of the Quantum Computer to use from the available quantum computers
= "SimulatorC"
BACKEND_NAME # the name of this service. For your own bookkeeping.
= "local"
SERVICE_NAME # the timeout in seconds for how long to keep checking for results
= 300 POLL_TIMEOUT
Get the Tergite Backend
The backend object can now be obtained. A detailed list of the backend properties — such as the available gate set, coupling map and number of qubits — is availablde by printing the backend object.
# provider account creation can be skipped in case you already saved
# your provider account to the `~/.qiskit/tergiterc` file.
# See below how that is done.
= ProviderAccount(service_name=SERVICE_NAME, url=API_URL, token=API_TOKEN)
account
= Tergite.use_provider_account(account)
provider # to save this account to the `~/.qiskit/tergiterc` file, add the `save=True`
# provider = Tergite.use_provider_account(account, save=True)
# Get the tergite backend in case you skipped provider account creation
# provider = Tergite.get_provider(service_name=SERVICE_NAME)
= provider.get_backend(BACKEND_NAME)
backend =1024)
backend.set_options(shotsprint(backend)
Create the Qiskit Circuit
To test our connection, we will implement a short test circuit. The circuit we will run produces the equal superposition state \(|\Psi\rangle = |0\rangle + |1\rangle\).
= circuit.QuantumCircuit(1)
qc 0) qc.h(
We can visualize and verify our circuit with Qiskit’s built in draw()
method. The output format of qc.draw()
can be changed, see https://docs.quantum.ibm.com/build/circuit-visualization. Note the added measurement and corresponding classical bit register meas_0
.
qc.draw()
To measure the prepared Bell state we add explicit measurements to all qubits using qc.measure_all()
. This will perform a meaurement in the so-called computational basis, \(\langle q|Z|q\rangle\), mapping the eigenvalues \(\{-1,1\}\) to the classical binary values \(\{0,1\}\). Drawing the final circuit shows the additional measurement operations and the classical bit register meas
.
qc.measure_all() qc.draw()
Compile Circuit
In order to execute the circuit on physical hardware, the circuit needs to be compiled (or transpiled) to the target architecture. At the least, transpilation accounts for the QPU’s native gate set and the qubit connectivity on the QPU. Many transpilers also offer some level of optimization, reducing the circuit size.
= compiler.transpile(qc, backend=backend)
tc tc.draw()
Run the Circuit
Once the cicruit has been compiled to the native gate set and connectivity, we use it to submit a job to the backend.
= backend.run(tc, meas_level=2, meas_return="single") job
See the Results
When the job has been submitted, we will need to wait potential queue time and time required to execute the job.
= 0
elapsed_time = None
result while result is None:
if elapsed_time > POLL_TIMEOUT:
raise TimeoutError(f"result polling timeout {POLL_TIMEOUT} seconds exceeded")
1)
time.sleep(+= 1
elapsed_time = job.result()
result
result.get_counts()
Acknowledgement
This notebook was prepared by: