Superstaq Basics in Cirq

Open in Colab Launch Binder

Here we demonstrate the basics of Superstaq by creating a circuit in Cirq and submitting to IBM’s Brisbane QPU. We assume you have a basic understanding of Python, as well as some familiarity with Cirq.

Import Superstaq and requirements

First, you will need to install and then import the packages required to allow Superstaq to run.

[1]:
%pip install --quiet 'cirq-superstaq[examples]'
print("You may need to restart the kernel to import newly installed packages.")
[2]:
# Requirements to use cirq-superstaq
import cirq
import cirq_superstaq as css

# Optional
import os  # Used to store a token in an environment variable

If you do not have the requirements already installed on your machine, you may add a code cell into the notebook to run %pip install <package_name> or run in terminal pip install <package_name>.

Set up access to Superstaq’s API

You will need to first grab your Superstaq API key. Here, we store the API key in an environment variable, but you may also pass your token to the api_key parameter of css.Service().

To set up an environment variable, run export SUPERSTAQ_API_KEY="<token>", where token is the API key you have retrieved from https://superstaq.infleqtion.com, in the terminal where you are using Superstaq. Alternatively, you can add a cell to this notebook and run !export SUPERSTAQ_API_KEY="<token>".

[3]:
# For cirq-superstaq: service to access Superstaq's API
service = css.Service()

Create a circuit

Now, use Cirq to create the circuit you desire. Here, we create a Bell state circuit.

[4]:
# Using Cirq
qubits = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
    cirq.H(qubits[0]),
    cirq.CNOT(qubits[0], qubits[1]),
    cirq.measure(qubits[0]),
    cirq.measure(qubits[1]),
)
print(circuit)
0: ───H───@───M───
          │
1: ───────X───M───

Submit your circuit and view results

Finally, we can submit our circuit to the desired device (by specifying target=) and view the results of our job.

Here, we will simulate for IBM’s Brisbane QPU, but you can access, compile, and simulate to many other devices via Superstaq!

To perform the simulation, we must instruct Superstaq to simulate the circuit to the desired target backend by passing "dry-run" as the method. Simulation via "dry-run" is a feature that is available to all users including free-trial users! Finally, we can retrieve the results of the job, the counts, by calling counts().

[5]:
# Specify "dry-run" as the method to submit & run a Superstaq simulation
job = service.create_job(
    circuit,
    method="dry-run",
    target="ibmq_brisbane_qpu",
    repetitions=100,
)
result = job.counts(0)
print(result)
{'00': 45, '11': 55}