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.
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)]
# prints "(0, 0)"
cz01 = cirq.CZ(qubits, qubits)
x2 = cirq.X(qubits)
cz12 = cirq.CZ(qubits, qubits)
moment0 = cirq.Moment([cz01, x2])
moment1 = cirq.Moment([cz12])
circuit = cirq.Circuit((moment0, moment1))
# prints the text diagram for the circuit:
# (0, 0): ───@───────
# (0, 1): ───@───@───
# (0, 2): ───X───@───
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, device.qubits), X(device.qubits)])
schedule = cirq.moment_by_moment_schedule(device, circuit)
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))
# CNOT((0, 0), (0, 1))
# CNOT((0, 0), (0, 1))
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)
# prints something like
# q0=1 q1=1
Even though Cirq was just announced yesterday, it already includes an all-star group of early adopters:
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.