terminal
Docs

Using Native Gates with Cirq

Last updated: January 30, 2024
IonQ Staff

The IonQ hardware-native gate set─previously only available to select research partners and customers─ is now enabled for anyone who accesses IonQ Quantum Cloud via an IonQ-provided API token, including those who access IonQ hardware through Google Cloud Marketplace. This feature is available on all QPUs that you might have access to, as well as our simulation service.

As part of this rollout, we’ve also added native gate support to several of the leading open-source software development kits in the quantum space, including Cirq. This guide explains how to use native gates with Cirq with a basic example.

We recommend reading our main Getting Started with Native Gates guide and our Get Started With Cirq on IonQ Hardware guide as prerequisites to this guide.

WARNING: This is an advanced-level feature. Using the hardware-native gate interface without a thorough understanding of quantum circuits is likely to result in less-optimal circuit structure and worse algorithmic performance overall than using our abstract gate interface.

Native Gates in Cirq

Note that as of May 2022, native gate support is in Cirq’s pre-release build, but not yet in a stable release.

You can install Cirq nightly by running

pip install --pre cirq

You may have to pip uninstall cirq first if you already have the package installed.

The native gates themselves are provided as part of the cirq_ionq sub-package:

import cirq
import cirq_ionq as ionq
from cirq_ionq.ionq_native_gates import GPIGate, GPI2Gate, MSGate

Using them is the same as using any other gate in Cirq:

q0, q1, q2 = cirq.LineQubit.range(3)
gpi = GPIGate(phi=0.5).on(q0)
gpi2 = GPI2Gate(phi=0.5).on(q1)
ms = MSGate(phi0=0, phi1=0.5).on(q1, q2)
meas = cirq.measure(q0, q1, q2, key='output')
circuit = cirq.Circuit([gpi, gpi2, ms, meas])
print(circuit)
0: ───GPI(0.5)──────────────M('output')───
│
1: ───GPI2(0.5)───MS(0)─────M─────────────
│ │
2: ───────────────MS(0.5)───M─────────────

Note that cirq.ion.ion_gates.MSGate is an arbitrary-angle MS, which takes a different parameter signature and is not equivalent to the IonQ Native MS gate.

Running on the IonQ Service

There is no special configuration for submission; the Cirq serializer auto-detects which gate set you are using. Note that if you use a mixed gate set, it will result in an error.

service = ionq.Service(api_key=YOUR_API_KEY)
result = service.run(circuit=circuit, repetitions=1024, target="simulator")  
print(result.histogram(key="output"))
Counter({5: 274, 7: 255, 6: 252, 4: 243})

Transpilation to Native Gates in Cirq

Our native gates are currently not structured in a way that allows you to automatically transpile a circuit into native gates via the Cirq compiler — we plan to in the future. If you’d like to help, feel free to open an issue or a pull request on the Cirq's IonQ module.

For now, we recommend transpiling using the steps described in our native gates guide.

QIS Gates in Cirq

When updating to pre-release Cirq to use native gates, there is a minor change in submission to QIS circuits. This is because in the latest Cirq, there is no longer an implicit decomposition to device gates.

You must now explicitly trigger this decomposition, which you can do by changing your decomposition code from

circuit = cirq.Circuit(device=device)

to the following:

final_circuit = cirq.Circuit(
    [ ionq.decompose_to_device(op) for op in circuit.all_operations()]
)

Additional Resources

Be sure to read our main Getting Started with Native Gates guide and our Get Started With Cirq on IonQ Hardware guide for more context on Native Gates and Cirq, respectively.

Native gates are also available in other open source tools, including PennyLane and Qiskit — see Using Native Gates With PennyLane and Using Native Gates With Qiskit for more.