Notebook template

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

Setup

try:
    import cirq
    import cirq_google
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq-google~=1.0.dev
    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 https://github.com/quantumlib/Cirq.git

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
try: 
    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(engine.list_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()
    print("Error message:")
    print(err)
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:")
    print(err)
Successful authentication using project !
Available Processors: 
[<cirq_google.engine.simulated_local_processor.SimulatedLocalProcessor object at 0x7f7cd88669e0>, <cirq_google.engine.simulated_local_processor.SimulatedLocalProcessor object at 0x7f7cd88a0b80>]
Using processor: rainbow

Example

# 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(
    program=circuit,
    repetitions=1000)

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