Cirq - Google Quantum AI
Overview
Google Quantum AI is proceeding the quantum computing and developing the tools for researchers to operate far away from classical capabilities.
Cirq is Google’s quantum computing development library. It is a Python library for creating, manipulating, and optimizing quantum circuits and running them against quantum computers and simulators.
Cirq also provides generalization for dealing with today’s noisy intermediate-scale quantum computers.
Features
- Circuits
- Devices
- Simulation
Circuits
It covers how to build quantum circuits from gates acting on qubits.
Circuit construction
The key representation of quantum programs in Cirq is the circuit class. A circuit is basically a collection of Moments. And a Moment is a collection of Operations that acts during the same abstract time.So we can say Operation is some effect that operates on a specific subset of Qubits; the most common type of Operation is a GateOperation.
Qubit types
There are 3 main qubit types in Cirq:
1- cirq.NamedQubit- It is an abstract qubit that has a name only , nothing else. We can use this when we don't need anything else and you don't need to create too many qubits in bulk.
2- cirq.LineQubit - In this a qubit is identified by an integer index in a line. Some devices have lines of qubits, LineQubit can be useful to represent that.
3- cirq.GridQubit- In this a qubit is placed on a grid and is identified by the 2D coordinates. Most of Google's chips are represented using GridQubit.
Gates and Operations
Quantum gates to apply to qubits in a circuit.
QE Gates Cirq Translation
U(theta,phi,lemda) QasmUGate(theta,phi,lemda)
CX cirq.cx()
U3 QasmUGate(theta,phi,lemda)
U2 QasmUGate(pi/2,phi,lemda)
ID cirq.Identity
x cirq.X
y cirq.Y
Z cirq.Z
h cirq.H
s cirq.S
sdg cirq.S**-1
ttg cirq.T**-1
rx(theta) cirq.Rx(theta)
ry(theta) cirq.Ry(theta)
rz(theta) cirq.Rz(theta)
cx cirq.CX()
cy cirq.ControlledGate(cirq.Y)
cz cirq.CZ
ch cirq.ControlledGate(cirq.H)
swap cirq.SWAP
ccx cirq.CCX
cswap cirq.CSWAP
Supported control statements
Custom Gates
Standard gates such as Pauli gates and CNOT are defined in cirq.ops.To use a unitary which is not a standard gate in a circuit, one can create a custom gate.So we can create our own gates with unitaries or decomposition.
General pattern
Gates are classes in Cirq. To define custom gates, we inherit from a base gate class and define a few methods.
The general pattern is:
- to inherit from cirq.Gate.
- Define one of the _num_qubits_ or _qid_shape_ methods.
- Define one of the _unitary_ or _decompose_ methods.
Import/Export Circuits
Importing or exporting circuits into/out of Cirq.And Exporting and importing to JSON can be done.In order to store circuits or transfering them between collaborators, JSON can be a good choice. Many objects in cirq can be issued as JSON and then stored as a text file for transfer, or storage.
Devices
Hardware constraints have a large impact on whether a circuit is practical or not on modern hardware. Let's see how devices can be defined to handle these constraints, including how to program Google’s Quantum Computing Service.
Run a circuit on a hardware device
Cirq provides interfaces for running the circuits on quantum hardware provided by many different services.
1-AQT hardware
It is the Cirq's interface with Alpine Quantum Technologies hardware.
2-Azure Quantum
It is the Cirq's interface with Microsoft Azure Quantum services.
3-IonQ hardware
It is the Cirq's interface with IonQ hardware.
4-Pasqal hardware
It is the Cirq's interface with Pasqal hardware.
5-Rigetti hardware
It s the Cirq's interface with Rigetti hardware.
Simulation
Cirq comes with built-in simulators, available for both wave functions and for density matrices. These can be use to handle noisy quantum channels using monte carlo or full density matrix simulations.
Cirq works with a state-of-the-art wavefunction simulator i.e. qsim. These simulators basically can be used to mock quantum hardware with the Quantum Virtual Machine(QVM).
Simulate a circuit
By simulating a quantum computer with a classical one, we can compute the effects of a quantum circuit.
Simulate with Cirq
1-Exact Simulation
Simulate a perfectly noiseless quantum computer.
2-Noisy Simulation
Simulate a more realistic quantum computer, subject to error and noise.
3-Parameter Sweeps
Efficiently evaluate many circuits which only differ in operation parameter values.
4-State Histograms
Visualize the results of simulation as a histogram over the basis states.
Quantum Virtual Machine
We can run circuits on a virtual version of quantum hardware, which completes with an identical interface and noisy simulation that mimics hardware devices.
We can build and use a QVM with a virtual Engine and realistic noise model.
Simulate with QVM
The Quantum Virtual Machine (QVM) mimics the experience and the results of programming one of the quantum computers in our lab.And the measurements feeds from the Sycamore processors, such as qubit decay, dephasing, gate and readout errors into the QVM and combine these with the qubit connectivity of the device to simulate quantum processor-like output.
The Quantum Virtual Machine can be deployed instantly from a Colab Notebook and is available free of cost.
Now,what is Colab?!!
So, Yes.... Colab, or "Colaboratory", lets us to write and execute Python in the browser, with
- Zero configuration required
- Access to GPUs free of charge
- Easy sharing
Whether one is a student, a data scientist or an AI researcher, Colab can makes the work easier.
Simply by clicking on create new notebook.It will create a notebook and there we can write,run program and visualize the result.
Most importantly we do not have to wait in a queue to get your program’s results and can iterate on results quickly. This, combined with processor-like output makes the QVM a great tool for prototyping, testing and optimizing the quantum circuit for near term quantum hardware
We can currently imitate two of the processors: Weber and Rainbow.
Weber is the Sycamore processor that was used in our beyond classical experiments.
Rainbow was used in the experiments demonstrating the variational quantum eigensolver on quantum chemistry problems.
Simulate with Qsim
It is an Optimized quantum circuit simulator. It is a full wave function simulator written in C++. It is integrated with Cirq and can be used to run simulations of up to 40 qubits.
All these is the complete overview about Google Quantum Computing Platform i.e Cirq.
Now let's try it experimentally.
Start —>Build —> Simulate
Local Installation: on Windows
1- First Make sure you have python latest version or greater than version 3.7.0
2- Use pip
to install cirq
:
python -m pip install --upgrade pip
python -m pip install cirq
3- Check if installed successfully !Try it by running the sample code given below:
python -c "import cirq_google; print(cirq_google.Sycamore)"
Now let's try to run small program and check the result out there.
The circuit is printed out in ASCII text, as it is standard for drawing circuits in Cirq, and the results are indicated by a sequence of binary digits.
Launch JupyterNotebook
Let's try to Simulate a 2-qubit "Bell State"
Step-1: Create a circuit to generate a Bell State:
1/sqrt(2) * ( |00⟩ - i |11⟩ )
Step -2: Initialize the simulator
Step-3 : Print result.
Here you can see it shows measurement.
Note: The keyword argument key='m’ in the measure operation makes it easy to access measurement results using the histogram method.
Plot histogram :
Repititions : The number of samples to take.
State Histograms are useful to visualize the output of running a quantum circuit.Plot the state histogram from either a single result with repetitions.
Let's try to run it on a hardware devices.
And I am choosing one of the them.
AQT (Alpine Quantum Technologies) hardware.
AQT supports Cirq as a third party software development kit and offers access to various quantum computing devices and simulators in the backend.
Requires Sign in and then get subscription.
We can use different backends by the use of a token and the corresponding backend URL like in the following picture.
The AQT simulators are capable of running ideal simulations (without a noise model) and real simulations (with a noise model) of a quantum circuit. When we use simulator with noise model it will allows us to estimate the performance of running a circuit on the real hardware. Switching between the two simulation types is done by using the respective Backend URL .
When we were using local simulator we have added measurement opration to the circuit to get the result.But here we don't require to add measurement as we are going to get through hardware device.
Now next step, simply we will start with importing some libraries
AQT sampler connects to the AQT machine and runs a single circuit or an entire sweep remotely.
Access token as well as url we have to pass for the remote api and both of these will get with subscription as shown in above picture.
Next in the arguments we have to pass : the circuit to sample from , the number of repetitions to simulate.This circuit will be executed a number of times to sample specified in the repetitions attribute
Result only contains measurement data, and the complete state vector is hidden.