Notebook template

View on QuantumAI Run in Google Colab View source on GitHub Download notebook


    import cirq
    import cirq_google
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq-google --pre
    print("installed cirq.")
    import cirq
    import cirq_google

Make a copy of this template

You will need to have access to Quantum Computing Service before running this colab.

This notebook can serve as a starter kit for you to run programs on Google's quantum hardware. You can download it using the directions below, open it in colab (or Jupyter), and modify it to begin your experiments.

How to download iPython notebooks from GitHub

You can retrieve iPython notebooks in the Cirq repository by going to the docs/ directory. For instance, this Colab template is found here. Select the file that you would like to download and then click the Raw button in the upper-right part of the window:

GitHub UI button to view raw file

This will show the entire file contents. Right-click and select Save as to save this file to your computer. Make sure to save to a file with a .ipynb extension (you may need to select All files from the format dropdown instead of text). You can also get to this Colab's raw content directly

You can also retrieve the entire Cirq repository by running the following command in a terminal that has git installed:

git checkout

How to open Google Colab

You can open a new Colab notebook from your Google Drive window or by visiting the Colab site. From the Colaboratory site, you can use the menu to upload an iPython notebook:

Google Colab's upload notebook entry in File menu.

This will upload the ipynb file that you downloaded before. You can now run all the commands, modify it to suit your goals, and share it with others.

Authenticate with Quantum Computing Service and install Cirq

For details of authentication and installation, please see Get started with Quantum Computing Service.

  1. Enter the Cloud project ID you'd like to use in the project_id field.
  2. Then run the cell below (and go through the auth flow for access to the project id you entered).

Quantum Engine console

# The Google Cloud Project id to use.
project_id = ''
processor_id = ""

from cirq_google.engine.qcs_notebook import get_qcs_objects_for_notebook
# For real engine instances, delete 'virtual=True' below.
qcs_objects = get_qcs_objects_for_notebook(project_id, processor_id, virtual=True)
engine = qcs_objects.engine
processor_id = qcs_objects.processor_id
Available processors: ['rainbow', 'weber']
Using processor: rainbow

Create an Engine variable

The following creates an engine variable which can be used to run programs under the project ID you entered above.

from google.auth.exceptions import DefaultCredentialsError
from google.api_core.exceptions import PermissionDenied

# Create an Engine object to use, providing the project id and the args
    if qcs_objects.signed_in: # This line only needed for colab testing.
        engine = cirq_google.get_engine()
    print(f"Successful authentication using project {project_id}!")
    print('Available Processors: ')
    print(f'Using processor: {processor_id}')
    processor = engine.get_processor(processor_id)
except DefaultCredentialsError as err: 
    print("Could not authenticate to Google Quantum Computing Service.")
    print(" Tips: If you are using Colab: make sure the previous cell was executed successfully.")
    print("       If this notebook is not in Colab (e.g. Jupyter notebook), make sure gcloud is installed and `gcloud auth application-default login` was executed.")
    print("Error message:")
except PermissionDenied as err:
    print(f"While you are authenticated to Google Cloud it seems the project '{project_id}' does not exist or does not have the Quantum Engine API enabled.")
    print("Error message:")
Successful authentication using project !
Available Processors: 
[<cirq_google.engine.simulated_local_processor.SimulatedLocalProcessor object at 0x7f8587111430>, <cirq_google.engine.simulated_local_processor.SimulatedLocalProcessor object at 0x7f85870d4dc0>]
Using processor: rainbow


# A simple example.
q = cirq.GridQubit(5, 2)
circuit = cirq.Circuit(cirq.X(q)**0.5, cirq.measure(q, key='m'))

job = processor.run_sweep(

results = [str(int(b)) for b in job.results()[0].measurements['m'][:, 0]]
print('Success!  Results:')
Success!  Results: