Jesus Rodriguez

Chief Scientist, Managing Partner at Invector Labs. CTO at IntoTheBlock. Angel Investor, Writer, Boa

Google Cirq and the New World of Quantum Programming

The race for quantum computing dominance is on with four protagonists: IBM, Microsoft, Alibaba and Google. Whereas most of the attention in the quantum world is focused on the new generation of super computers that are breaking computation records regularly, we shouldn’t forget about the developers that are going to be building the first wave of quantum applications. Microsoft took the initiative to attract developers to the quantum world by releasing the first version of its Quantum Developer Kit which even includes a new programming language called Q#. Yesterday, at the First International Workshop on Quantum Software and Quantum Machine Learning(QSML), Google decided to enter the race by releasing the first public alpha of Cirq, an open source framework that allow developers to create algorithms without needing a background in quantum physics.

Building applications for the quantum computers is full of challenges. For starters, developers need to get accustomed to the properties of “qubits” that differ from traditional bits in more than one annoying way 😉. For instance, qubits can be in both binary states [1,0] at the same time which is a phenomenon known as superposition. Additionally, qubits can influence each other even if they are not physically connected and their state changes incredibly rapidly. As a result, building quantum applications requires new types of programming models like Q# and now Cirq.

The idea behind Cirq is to provide a simple programming model that abstracts the fundamental building blocks of quantum applications. The initial version of Cirq has been implemented in Python and is available on Github. Architecturally, Cirq models a quantum application using a series of base constructs.

Circuits

Any quantum programin Cirq starts is abstracted by one of two fundamental classes: a Ciruit or a Schedule. In Cirq, a Cirquit[capital C] represents the most basic form of a quantum circuit[lowercase c]. A Cirq Circuit is represented as a collection of Moments which include operations that can be executed on Qubits during some abstract slide of time.

The syntax for combining Circuit, Moments and Operations in Cirq is fairly simple as shown in the following code.

qubits = [cirq.GridQubit(x, y) for x in range(3) for y in range(3)]

print(qubits[0])
# prints "(0, 0)"
cz01 = cirq.CZ(qubits[0], qubits[1])
x2 = cirq.X(qubits[2])
cz12 = cirq.CZ(qubits[1], qubits[2])
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))

print(circuit)
# prints the text diagram for the circuit:
# (0, 0): ───@───────
# │
# (0, 1): ───@───@───
# │
# (0, 2): ───X───@───

Devices

The Device construct in Cirq represents the constraints of a specific type of quantum hardware. For instance, most hardware only allows certain gates to be enacted on qubits. Or, as another example, some gates may be constrained to not be able to run at the same time as neighboring gates

Schedules & Devices

A Schedule is another form of quantum circuit that includes more detailed information about the timing and duration of the gates. Conceptually, a Schedule is made up of a set of ScheduledOperations as well as a description of the Device on which the schedule is intended to be run. Each ScheduledOperation is made up of a timewhen the operation starts and a duration describing how long the operation takes, in addition to the Operation itself.

In the following code, we take a Circuit on a specific type of Device and convert it to a Schedule using a single call:

from cirq.google.xmon_gates import ExpWGate
circuit = cirq.Circuit()
CZ = Exp11Gate(half_turns=1.0)
X = ExpWGate(half_turns=1.0)
circuit.append([CZ(device.qubits[0], device.qubits[1]), X(device.qubits[0])])
print(circuit)
schedule = cirq.moment_by_moment_schedule(device, circuit)

Gates

In Cirq, Gates abstract operations on collections of qubits. To apply a Gate on a qubit, we simply call its On method as shown in the following code:

from cirq.ops import CNOT
from cirq.devices import GridQubit
q0, q1 = (GridQubit(0, 0), GridQubit(0, 1))
print(CNOT.on(q0, q1))
print(CNOT(q0, q1))
# prints
# CNOT((0, 0), (0, 1))
# CNOT((0, 0), (0, 1))

Simulators

Cirq includes a Python simulator that can be used to run Circuits and Schedules. The Simulator architecture can scale across multiple threads and CPUs which allows it to run fairly sophisticated Circuits. The following code uses a Simulator modeled after the Google’s Xmom architecture:

from cirq.google import XmonSimulator
simulator = XmonSimulator()
result = simulator.run(circuit)

print(result)
# prints something like
# q0=1 q1=1

Early Adopters

Even though Cirq was just announced yesterday, it already includes an all-star group of early adopters:

· Zapata Computing: simulation of a quantum autoencoder (example code, video tutorial)

· QC Ware: QAOA implementation and integration into QC Ware’s AQUA platform (example code, video tutorial)

· Quantum Benchmark: integration of True-Q software tools for assessing and extending hardware capabilities (video tutorial)

· Heisenberg Quantum Simulations: simulating the Anderson Model

· Cambridge Quantum Computing: integration of proprietary quantum compiler t|ket> (video tutorial)

· NASA: architecture-aware compiler based on temporal-planning for QAOA (slides) and simulator of quantum computers (slides)

Additionally, Google announced the release of OpenFermion-Cirq. The framework uses a OpenFermion quantum chemistry platform and adds an open source library that compiles quantum simulation algorithms to Cirq.

Quantum computing requires a new type of programming language and efforts like Cirq and Microsoft’s Q# are the first steps to bring quantum computing to mainstream developers. The next version of Cirq is likely to include better tooling and libraries that abstract even more important constructs to build the next generation of quantum applications.

More by Jesus Rodriguez

Topics of interest

More Related Stories