Compiling and Submitting Circuits onto Sqorpius via Cirq
Import Requirements
This tutorial will showcase how to compile and submit a circuit onto Infleqtion’s hardware, Sqorpius, using the cirq-superstaq client.
[1]:
# Required imports
try:
import cirq
import cirq_superstaq as css
except ImportError:
print("Installing cirq-superstaq...")
%pip install --quiet 'cirq-superstaq[examples]'
print("Installed cirq-superstaq.")
print("You may need to restart the kernel to import newly installed packages.")
import cirq
import cirq_superstaq as css
# Optional imports
import os # Used if setting a token as an environment variable
To interface Superstaq via Cirq, we must first instantiate a service provider in cirq-superstaq with Service(). We then supply a Superstaq API key (which you can get from https://superstaq.infleqtion.com) by either providing the API key as an argument of Service, i.e., css.Service(api_key="token"), or by setting it as an environment variable. (see more details
here).
[2]:
service = css.Service()
Create a Circuit
First, we will create an example Cirq circuit that we can then compile and submit onto Sqorpius
[3]:
qubits = cirq.LineQubit.range(4)
circuit1 = cirq.Circuit(cirq.H(qubits[0]), cirq.CNOT(qubits[0], qubits[1]), cirq.measure(qubits[0]))
circuit1
[3]:
0: ───H───@───M───
│
1: ───────X───────Single Circuit Compilation
We will now compile the above circuit onto Sqorpius’s native gateset and visualize the differences by drawing the compiled circuit
[4]:
compiler_output = service.cq_compile(circuit1)
[5]:
compiler_output.circuit
[5]:
0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───
│ │ │ │
1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────
│ │ │
2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────
│ │ │
3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────
│ │ │
4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────
│ │ │
5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────
│ │ │
6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────
│ │ │
7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────
│ │ │
8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────
│ │ │
9: ────#10──────────────────────#10────────────────────────#10───────────────────────────
│ │ │
10: ───#11──────────────────────#11────────────────────────#11───────────────────────────
│ │ │
11: ───#12──────────────────────#12────────────────────────#12───────────────────────────
│ │ │
12: ───#13──────────────────────#13────────────────────────#13───────────────────────────
│ │ │
13: ───#14──────────────────────#14────────────────────────#14───────────────────────────
│ │ │
14: ───#15──────────────────────#15────────────────────────#15───────────────────────────
│ │ │
15: ───#16──────────────────────#16────────────────────────#16───────────────────────────
│ │ │
16: ───#17──────────────────────#17────────────────────────#17───────────────────────────
│ │ │
17: ───#18──────────────────────#18────────────────────────#18───────────────────────────
│ │ │
18: ───#19──────────────────────#19────────────────────────#19───────────────────────────
│ │ │
19: ───#20──────────────────────#20────────────────────────#20───────────────────────────
│ │ │
20: ───#21──────────────────────#21────────────────────────#21───────────────────────────
│ │ │
21: ───#22──────────────────────#22────────────────────────#22───────────────────────────
│ │ │
22: ───#23──────────────────────#23────────────────────────#23───────────────────────────
│ │ │
23: ───#24──────────────────────#24────────────────────────#24───────────────────────────If you would like to compile (or submit) on a different number of qubits, this can be done via the grid_shape option. This simply sets the shape of the rectangular qubit grid. However, specifying a grid that is incompatible with Sqorpius’s current capabilities will result in an error when submitting. Anything equal to or smaller than than (6,4) is compatible with Sqorpius’s current capabilities.
[6]:
example_circuit = cirq.Circuit(cirq.H.on_each(*qubits))
new_compiler_output = service.cq_compile(example_circuit, grid_shape=(4, 1))
new_compiler_output.circuit
[6]:
(0, 0): ───RGate(0.5π, -0.5π)───Z───
│
(1, 0): ───#2───────────────────Z───
│
(2, 0): ───#3───────────────────Z───
│
(3, 0): ───#4───────────────────Z───Single Circuit Submission
The code below will submitt he circuit to the noiseless Sqorpius simulator. If you would like to run on Sqorpius, change the target argument in job_css from cq_sqorpius_simulator to cq_sqorpius_qpu. It is recommended to first submit to the simulator for testing to ensure your code runs before submitting to Sqorpius.
[7]:
job_css = service.create_job(circuit1, repetitions=100, target="cq_sqorpius_simulator")
result_css = job_css.counts(0)
print(f"Counts from cirq-superstaq submission: {result_css}")
Counts from cirq-superstaq submission: {'1': 53, '0': 47}
If you would like to submit these circuits to a noisy simulator, to see the effects of noise on your simulated measurements without submitting to an actual device, you can also add in the argument method = noise-sim in job_css.
[8]:
job_css = service.create_job(
circuit1, repetitions=100, target="cq_sqorpius_simulator", method="noise-sim"
)
result_css = job_css.counts(0)
print(f"Counts from noisy cirq-superstaq submission: {result_css}")
Counts from noisy cirq-superstaq submission: {'0': 51, '1': 49}
Multiple circuit compilation
All the functionalities we have seen so far can also be used on a multiple-circuit input as well. To illustrate this, let us create a different example two-qubit circuit, and compile both circuits we have created at the same time.
[9]:
circuit2 = cirq.Circuit(cirq.H(qubits[0]), cirq.measure(qubits[0]))
circuit2
[9]:
0: ───H───M───
[10]:
compiler_output = service.cq_compile([circuit1, circuit2])
[11]:
compiler_output.circuits[0]
[11]:
0: ────RGate(0.5π, -0.5π)───@───RGate(-0.25π, -0.5π)───────RGate(0.25π, -0.5π)───────M───
│ │ │ │
1: ────#2───────────────────@───#2─────────────────────Z───#2────────────────────Z───────
│ │ │
2: ────#3───────────────────────#3─────────────────────────#3────────────────────────────
│ │ │
3: ────#4───────────────────────#4─────────────────────────#4────────────────────────────
│ │ │
4: ────#5───────────────────────#5─────────────────────────#5────────────────────────────
│ │ │
5: ────#6───────────────────────#6─────────────────────────#6────────────────────────────
│ │ │
6: ────#7───────────────────────#7─────────────────────────#7────────────────────────────
│ │ │
7: ────#8───────────────────────#8─────────────────────────#8────────────────────────────
│ │ │
8: ────#9───────────────────────#9─────────────────────────#9────────────────────────────
│ │ │
9: ────#10──────────────────────#10────────────────────────#10───────────────────────────
│ │ │
10: ───#11──────────────────────#11────────────────────────#11───────────────────────────
│ │ │
11: ───#12──────────────────────#12────────────────────────#12───────────────────────────
│ │ │
12: ───#13──────────────────────#13────────────────────────#13───────────────────────────
│ │ │
13: ───#14──────────────────────#14────────────────────────#14───────────────────────────
│ │ │
14: ───#15──────────────────────#15────────────────────────#15───────────────────────────
│ │ │
15: ───#16──────────────────────#16────────────────────────#16───────────────────────────
│ │ │
16: ───#17──────────────────────#17────────────────────────#17───────────────────────────
│ │ │
17: ───#18──────────────────────#18────────────────────────#18───────────────────────────
│ │ │
18: ───#19──────────────────────#19────────────────────────#19───────────────────────────
│ │ │
19: ───#20──────────────────────#20────────────────────────#20───────────────────────────
│ │ │
20: ───#21──────────────────────#21────────────────────────#21───────────────────────────
│ │ │
21: ───#22──────────────────────#22────────────────────────#22───────────────────────────
│ │ │
22: ───#23──────────────────────#23────────────────────────#23───────────────────────────
│ │ │
23: ───#24──────────────────────#24────────────────────────#24───────────────────────────[12]:
compiler_output.circuits[1]
[12]:
0: ────RGate(0.5π, -0.5π)───M───
│
1: ────#2───────────────────────
│
2: ────#3───────────────────────
│
3: ────#4───────────────────────
│
4: ────#5───────────────────────
│
5: ────#6───────────────────────
│
6: ────#7───────────────────────
│
7: ────#8───────────────────────
│
8: ────#9───────────────────────
│
9: ────#10──────────────────────
│
10: ───#11──────────────────────
│
11: ───#12──────────────────────
│
12: ───#13──────────────────────
│
13: ───#14──────────────────────
│
14: ───#15──────────────────────
│
15: ───#16──────────────────────
│
16: ───#17──────────────────────
│
17: ───#18──────────────────────
│
18: ───#19──────────────────────
│
19: ───#20──────────────────────
│
20: ───#21──────────────────────
│
21: ───#22──────────────────────
│
22: ───#23──────────────────────
│
23: ───#24──────────────────────