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:
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:
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.
More Documentation Links
Authenticate with Quantum Computing Service and install Cirq
For details of authentication and installation, please see Get started with Quantum Computing Service.
- Enter the Cloud project ID you'd like to use in the
project_id
field. - Then run the cell below (and go through the auth flow for access to the project id you entered).
# 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 0x7fba2ed3e8f0>, <cirq_google.engine.simulated_local_processor.SimulatedLocalProcessor object at 0x7fba2ed7cac0>] 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: 0010100111101110101111110001001011100100001111111111000100100100000001011100001111011001010101110110111011011010010110110100101000000001110110011100000010010000100011110001101010100010010001011111111101000001001001010110111000000011000110010001001110001011000011110010001110010000111001101100110011100110110010011011110101110001110100101000101000110010000010001101010100100101001111011010111001001001110010001010011011001011100101101011100001110010100000011111011110010011110000000010111110101101100111000100111001011001111111110110001011011010110010001001011000111010001100111000001101111010100000010000001101011100100010010000101100011111011011010111110010100101000110011010011110100011000101000100110010101011100000111000111100000111101010000100000101000101110110111011110010100101011111001001010000011111001111100111111010001000110010101001110101110000110010100001111001001000110110000110100101100111001011000011110001101010010010000010100100101011000011011101101111111100010101100000010110101111