Tutorial

In this tutorial we will go from knowing nothing about Cirq to creating a quantum variational algorithm. Note that this tutorial isn’t a quantum computing 101 tutorial, we assume familiarity of quantum computing at about the level of the textbook “Quantum Computation and Quantum Information” by Nielsen and Chuang.

To begin, please follow the instructions for installing Cirq.

Background: Variational quantum algorithms

The variational method in quantum theory is a classical method for finding low energy states of a quantum system. The rough idea of this method is that one defines a trial wave function (sometimes called an ansatz) as a function of some parameters, and then one finds the values of these parameters that minimize the expectation value of the energy with respect to these parameters. This minimized ansatz is then an approximation to the lowest energy eigenstate, and the expectation value serves as an upper bound on the energy of the ground state.

In the last few years (see arXiv:1304.3061 and arXiv:1507.08969 for example), it has been realized that quantum computers can mimic the classical technique and that a quantum computer does so with certain advantages. In particular, when one applies the classical variational method to a system of n qubits, an exponential number (in n) of complex numbers are necessary to generically represent the wave function of the system. However with a quantum computer one can directly produce this state using a parameterized quantum circuit, and then by repeated measurements estimate the expectation value of the energy.

This idea has led to a class of algorithms known as variational quantum algorithms. Indeed this approach is not just limited to finding low energy eigenstates, but minimizing any objective function that can be expressed as a quantum observable. It is an open question to identify under what conditions these quantum variational algorithms will succeed, and exploring this class of algorithms is a key part of research for noisy intermediate scale quantum computers.

The classical problem we will focus on is the 2D +/- Ising model with transverse field (ISING). This problem is NP-complete. So it is highly unlikely that quantum computers will be able to efficiently solve it across all instances. Yet this type of problem is illustrative of the general class of problems that Cirq is designed to tackle.

Consider the energy function

Energy: $E(s_1,dots,s_n) = sum_{<i,j>} J_{i,j}s_i s_j + sum_i h_i s_i$

where here each si, Ji,j, and hi are either +1 or -1. Here each index i is associated with a bit on a square lattice, and the <i,j> notation means sums over neighboring bits on this lattice. The problem we would like to solve is, given Ji,j, and hi, find an assignment of si values that minimize E.

How does a variational quantum algorithm work for this? One approach is to consider n qubits and associate them with each of the bits in the classical problem. This maps the classical problem onto the quantum problem of minimizing the expectation value of the observable

Hamiltonian: $H=sum_{<i,j>} J_{i,j} Z_i Z_j + sum_i h_iZ_i$

Then one defines a set of parameterized quantum circuits, i.e. a quantum circuit where the gates (or more general quantum operations) are parameterized by some values. This produces an ansatz state

State definition: $|psi(p_1, p_2, dots, p_k)rangle$

where pi are the parameters that produce this state (here we assume a pure state, but mixed states are of course possible).

The variational algorithm then works by noting that one can obtain the value of the objective function for a given ansatz state by

  1. Prepare the ansatz state.
  2. Make a measurement which samples from some terms in H.
  3. Goto 1.

Note that one cannot always measure H directly (without the use of quantum phase estimation). So one often relies on the linearity of expectation values to measure parts of H in step 2. One always needs to repeat the measurements to obtain an estimate of the expectation value. How many measurements needed to achieve a given accuracy is beyond the scope of this tutorial, but Cirq can help investigate this question.

The above shows that one can use a quantum computer to obtain estimates of the objective function for the ansatz. This can then be used in an outer loop to try to obtain parameters for the the lowest value of the objective function. For these values, one can then use that best ansatz to produce samples of solutions to the problem which obtain a hopefully good approximation for the lowest possible value of the objective function.

Create a circuit on a Grid

To build the above variational quantum algorithm using Cirq, one begins by building the appropriate circuit. In Cirq circuits are represented either by a Circuit object or a Schedule object. Schedules offer more control over quantum gates and circuits at the timing level, which we do not need, so here we will work with Circuits instead.

Conceptually: a Circuit is a collection of Moments. A Moment is a collection of Operations that all act during the same abstract time slice. An Operation is a an effect that operates on a specific subset of Qubits. The most common type of Operation is a Gate applied to several qubits (a GateOperation). The following diagram should help illustrate these concepts.

Circuits and Moments

Because the problem we have defined has a natural structure on a grid, we will use Cirq’s built in GridQubits as our qubits. We will demonstrate some of how this works in an interactive Python environment, the following code can be run in series in a Python environment where you have Cirq installed.

Let’s begin by talking about our qubits. In an interactive Python environment run

Here we see that we’ve created a bunch of GridQubits. GridQubits implement the QubitId class, which just means that they are equatable and hashable. QubitIdhas an abstract _comparison_key method that must be implemented by child types in order to ensure there’s a reasonable sorting order for diagrams and that this matches what happens when sorted(qubits) is called.GridQubits in addition have a row and column, indicating their position on a grid.

Now that we have some qubits, let us construct a Circuit on these qubits. For example, suppose we want to apply the Hadamard gate H to every qubit whose row index plus column index is even and an X gate to every qubit whose row index plus column index is odd. To do this we write

One thing to notice here. First cirq.X is a Gate object. There are many different gates supported by Cirq. A good place to look at gates that are defined is in common_gates.py. One common confusion to avoid is the difference between a gate class and a gate object (which is an instantiation of a class). The second is that gate objects are transformed into Operations (technically GateOperations) via either the method on(qubit) or, as we see for the X gates, via simply applying the gate to the qubits (qubit). Here we only apply single qubit gates, but a similar pattern applies for multiple qubit gates with a sequence of qubits as parameters.

Another thing one notices about the above circuit is that the circuit has staggered gates. This is because the way in which we have applied the gates has created two Moments.

Here we see that we can iterate over a Circuit’s Moments. The reason that two Moments were created was that the append method uses an InsertStrategy of NEW_THEN_INLINE. InsertStrategys describe how new insertions into Circuits place their gates. Details of these strategies can be found in the circuit documentation. If we wanted to insert the gates so that they form one Moment, we could instead use the EARLIEST insertion strategy:

We now see that we have only one moment, as the X gates have been slid over to act at the earliest Moment they can.

Creating the Ansatz

If you look closely at the circuit creation code above you will see that we applied the append method to both a generator and a list (recall that in Python one can use generator comprehensions in method calls). Inspecting the code for append one sees that the append method generally takes an OP_TREE (or a Moment). What is an OP_TREE? It is not a class but a contract. Roughly an OP_TREE is anything that can be flattened, perhaps recursively, into a list of operations, or into a single operation. Examples of an OP_TREE are

  • A single Operation.
  • A list of Operations.
  • A tuple of Operations.
  • A list of a list of Operationss.
  • A generator yielding Operations.

This last case yields a nice pattern for defining sub-circuits / layers: define a function that takes in the relevant parameters and then yields the operations for the sub circuit and then this can be appended to the Circuit:

Another important concept here is that the rotation gate is specified in “half turns”. For a rotation about X this is the gate cos(half_turns * pi) I + i sin(half_turns * pi) X.

There is a lot of freedom defining a variational ansatz. Here we will do a variation on a QOAO strategy and define an ansatz related to the problem we are trying to solve.

First we need to choose how the instances of the problem are represented. These are the values J and h in the Hamiltonian definition. We will represent these as two dimensional arrays (lists of lists). For J we will use two such lists, one for the row links and one for the column links.

Here is code that we can use to generate random problem instances

where the actual values will be different for an individual run because they are using random.choice.

Given this definition of the problem instance we can now introduce our ansatz. Our ansatz will consist of one step of a circuit made up of

  1. Apply an XPowGate for the same parameter for all qubits. This is the method we have written above.
  2. Apply a ZPowGate for the same parameter for all qubits where the transverse field term h is +1.
  1. Apply a CZPowGate for the same parameter between all qubits where the coupling field term J is +1. If the field is -1 apply CZPowGate conjugated by X gates on all qubits.

Putting this together we can create a step that uses just three parameters. The code to do this uses the generator for each of the layers (note to advanced Python users that this code is not a bug in using yield due to the auto flattening of the OP_TREE concept. Normally one would want to use yield from here, but this is not necessary):

Here we see that we have chosen particular parameter values (0.1, 0.2, 0.3).

Simulation

Now let’s see how to simulate the circuit corresponding to creating our ansatz. In Cirq the simulators make a distinction between a “run” and a “simulation”. A “run” only allows for a simulation that mimics the actual quantum hardware. For example, it does not allow for access to the amplitudes of the wave function of the system, since that is not experimentally accessible. “Simulate” commands, however, are more broad and allow different forms of simulation. When prototyping small circuits it is useful to execute “simulate” methods, but one should be wary of relying on them when run against actual hardware.

Currently Cirq ships with a simulator tied strongly to the gate set of the Google xmon architecture. However, for convenience, the simulator attempts to automatically convert unknown operations into XmonGates (as long as the operation specifies a matrix or a decomposition into XmonGates). This can in principle allows us to simulate any circuit that has gates that implement one and two qubit KnownMatrixgates. Future releases of Cirq will expand these simulators.

Because the simulator is tied to the xmon gate set, the simulator lives, in contrast to core Cirq, in the cirq.google module. To run a simulation of the full circuit we simply create a simulator, and pass the circuit to the simulator.

Note that we have run the simulation 100 times and produced a histogram of the counts of the measurement results. What are the keys in the histogram counter? Note that we have passed in the order of the qubits. This ordering is then used to translate the order of the measurement results to a register using a big endianrepresentation.

For our optimization problem we will want to calculate the value of the objective function for a given result run. One way to do this is use the raw measurement data from the result of simulator.run. Another way to do this is to provide to the histogram a method to calculate the objective: this will then be used as the key for the returned Counter.

One can then calculate the expectation value over all repetitions

Parameterizing the Ansatz

Now that we have constructed a variational ansatz and shown how to simulate it using Cirq, we can now think about optimizing the value. On quantum hardware one would most likely want to have the optimization code as close to the hardware as possible. As the classical hardware that is allowed to inter-operate with the quantum hardware becomes better specified, this language will be better defined. Without this specification, however, Cirq also provides a useful concept for optimizing the looping in many optimization algorithms. This is the fact that many of the value in the gate sets can, instead of being specified by a float, be specified by a Symbol and this Symbol can be substituted for a value specified at execution time.

Luckily for us, we have written our code so that using parameterized values is as simple as passing Symbol objects where we previously passed float values.

Note now that the circuit’s gates are parameterized.

Parameters are specified at runtime using a ParamResolver which is which is just a dictionary from Symbol keys to runtime values. For example,

resolves the parameters to actual values in the above circuit.

More usefully, Cirq also has the concept of a “sweep”. A sweep is essentially a collection of parameter resolvers. This runtime information is very useful when one wants to run many circuits for many different parameter values. Sweeps can be created to specify values directly (this is one way to get classical information into a circuit), or a variety of helper methods. For example suppose we want to evaluate our circuit over an equally spaced grid of parameter values. We can easily create this using LinSpace.

Finding the Minimum

Now we have all the code to we need to do a simple grid search over values to find a minimal value. Grid search is most definitely not the best optimization algorithm, but is here simply illustrative.

We’ve created a simple variational quantum algorithm using Cirq. Where to go next? Perhaps you can play around with the above code and work on analyzing the algorithms performance. Add new parameterized circuits and build an end to end program for analyzing these circuits.