diff --git a/cirq-google/cirq_google/engine/qcs_notebook.py b/cirq-google/cirq_google/engine/qcs_notebook.py index f2b6dc7ca10..10549ba392a 100644 --- a/cirq-google/cirq_google/engine/qcs_notebook.py +++ b/cirq-google/cirq_google/engine/qcs_notebook.py @@ -90,35 +90,40 @@ def get_qcs_objects_for_notebook( # set or `gcloud auth application-default login` was executed # already. For more information on using Application Default Credentials # see https://cloud.google.com/docs/authentication/production - try: - from google.colab import auth - except ImportError: - print("Not running in a colab kernel. Will use Application Default Credentials.") - else: - print("Getting OAuth2 credentials.") - print("Press enter after entering the verification code.") - try: - auth.authenticate_user(clear_output=False) - print("Authentication complete.") - except Exception as exc: - print(f"Authentication failed: {exc}") - # Attempt to connect to the Quantum Engine API, and use a simulator if unable to connect. - if virtual: - engine: AbstractEngine = create_noiseless_virtual_engine_from_latest_templates() - signed_in = False - is_simulator = True - else: + if not virtual: + # Set up auth + try: + from google.colab import auth + except ImportError: + print("Not running in a colab kernel. Will use Application Default Credentials.") + else: + print("Getting OAuth2 credentials.") + print("Press enter after entering the verification code.") + try: + a = auth.authenticate_user(clear_output=False) + print(a) + print("Authentication complete.") + except Exception as exc: + print(f"Authentication failed: {exc}") + print(f"Using virtual engine instead.") + virtual = True + + if not virtual: + # Set up production engine try: - engine = get_engine(project_id) + engine: AbstractEngine = get_engine(project_id) signed_in = True is_simulator = False except Exception as exc: print(f"Unable to connect to quantum engine: {exc}") print("Using a noisy simulator.") - engine = create_noiseless_virtual_engine_from_latest_templates() - signed_in = False - is_simulator = True + virtual = True + if virtual: + engine = create_noiseless_virtual_engine_from_latest_templates() + signed_in = False + is_simulator = True + if processor_id: processor = engine.get_processor(processor_id) else: diff --git a/cirq-google/cirq_google/engine/qcs_notebook_test.py b/cirq-google/cirq_google/engine/qcs_notebook_test.py index ef4ce6cc4ba..0cfeeb2be4d 100644 --- a/cirq-google/cirq_google/engine/qcs_notebook_test.py +++ b/cirq-google/cirq_google/engine/qcs_notebook_test.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys import unittest.mock as mock import pytest @@ -95,3 +96,39 @@ def test_get_qcs_objects_for_notebook_no_processors(engine_mock): engine_mock.return_value = fake_engine with pytest.raises(ValueError, match='processors'): _ = get_qcs_objects_for_notebook() + + +@mock.patch.dict('sys.modules', {'google.colab': mock.Mock()}) +@mock.patch('cirq_google.engine.qcs_notebook.get_engine') +def test_get_qcs_objects_for_notebook_auth_succeeds(engine_mock): + fake_processor = cg.engine.SimulatedLocalProcessor( + processor_id='tester', project_name='mock_project', device=cg.Sycamore + ) + fake_engine = cg.engine.SimulatedLocalEngine([fake_processor]) + engine_mock.return_value = fake_engine + result = get_qcs_objects_for_notebook() + _assert_correct_types(result) + assert result.signed_in + assert not result.is_simulator + assert result.project_id == 'mock_project' + assert len(result.device.metadata.qubit_set) == 54 + + +@mock.patch.dict('sys.modules', {'google.colab': mock.Mock()}) +@mock.patch('cirq_google.engine.qcs_notebook.get_engine') +def test_get_qcs_objects_for_notebook_auth_fails(engine_mock): + auth_mock = sys.modules['google.colab'] + + auth_mock.auth.authenticate_user = mock.Mock(side_effect=Exception('mock auth failure')) + fake_processor = cg.engine.SimulatedLocalProcessor( + processor_id='tester', project_name='mock_project', device=cg.Sycamore + ) + fake_engine = cg.engine.SimulatedLocalEngine([fake_processor]) + engine_mock.return_value = fake_engine + result = get_qcs_objects_for_notebook() + + # Auth failed, default to simulator + _assert_correct_types(result) + assert not result.signed_in + assert result.is_simulator + assert result.project_id == 'fake_project' diff --git a/dev_tools/notebooks/notebook_test.py b/dev_tools/notebooks/notebook_test.py index f6b0e4707a6..ebe08b275b0 100644 --- a/dev_tools/notebooks/notebook_test.py +++ b/dev_tools/notebooks/notebook_test.py @@ -28,19 +28,23 @@ SKIP_NOTEBOOKS = [ # skipping vendor notebooks as we don't have auth sorted out - "**/aqt/*.ipynb", - "**/azure-quantum/*.ipynb", - "**/ionq/*.ipynb", - "**/google/*.ipynb", - "**/pasqal/*.ipynb", - "**/rigetti/*.ipynb", + '**/aqt/*.ipynb', + '**/azure-quantum/*.ipynb', + '**/ionq/*.ipynb', + '**/pasqal/*.ipynb', + '**/rigetti/*.ipynb', # skipping fidelity estimation due to # https://github.com/quantumlib/Cirq/issues/3502 - "examples/*fidelity*", + 'examples/*fidelity*', # tutorials that use QCS and arent skipped due to one or more cleared output cells + 'docs/tutorials/google/identifying_hardware_changes.ipynb', + 'docs/tutorials/google/echoes.ipynb', 'docs/noise/qcvv/xeb_calibration_example.ipynb', 'docs/noise/calibration_api.ipynb', 'docs/noise/floquet_calibration_example.ipynb', + # temporary: need to fix QVM metrics and device spec + 'docs/tutorials/google/spin_echoes.ipynb', + 'docs/tutorials/google/visualizing_calibration_metrics.ipynb', # shouldn't have outputs generated for style reasons 'docs/simulate/qvm_builder_code.ipynb', ] diff --git a/docs/tutorials/google/colab.ipynb b/docs/tutorials/google/colab.ipynb index 0bde770e5b6..808cefbfbf4 100644 --- a/docs/tutorials/google/colab.ipynb +++ b/docs/tutorials/google/colab.ipynb @@ -11,20 +11,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "cellView": "form", "id": "2u6HZdKb4RXV" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", @@ -82,19 +74,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "id": "bd9529db1c0b" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "try:\n", " import cirq\n", @@ -184,28 +168,20 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { - "cellView": "both", "id": "wfHdoHW67EGh" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Notebook is not executed with Colab, assuming Application Default Credentials are setup.\n", - "Successful authentication to Google Cloud.\n" - ] - } - ], + "outputs": [], "source": [ "# The Google Cloud Project id to use.\n", "project_id = '' #@param {type:\"string\"}\n", "processor_id = \"\" #@param {type:\"string\"}\n", "\n", "from cirq_google.engine.qcs_notebook import get_qcs_objects_for_notebook\n", - "device_sampler = get_qcs_objects_for_notebook(project_id, processor_id)" + "qcs_objects = get_qcs_objects_for_notebook(project_id, processor_id)\n", + "engine = qcs_objects.engine\n", + "processor_id = qcs_objects.processor_id" ] }, { @@ -221,28 +197,24 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "id": "HCb_9m_o8KEK" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "from google.auth.exceptions import DefaultCredentialsError\n", "from google.api_core.exceptions import PermissionDenied\n", "\n", "# Create an Engine object to use, providing the project id and the args\n", "try: \n", - " engine = cirq_google.get_engine()\n", - " engine.list_processors()\n", + " if qcs_objects.signed_in: # This line only needed for colab testing.\n", + " engine = cirq_google.get_engine()\n", " print(f\"Successful authentication using project {project_id}!\")\n", + " print('Available Processors: ')\n", + " print(engine.list_processors())\n", + " print(f'Using processor: {processor_id}')\n", + " processor = engine.get_processor(processor_id)\n", "except DefaultCredentialsError as err: \n", " print(\"Could not authenticate to Google Quantum Computing Service.\")\n", " print(\" Tips: If you are using Colab: make sure the previous cell was executed successfully.\")\n", @@ -267,30 +239,19 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "id": "xujtwGxt8fOA" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Success! Results:\n", - "0000001111010100111111000100000000011100001000000011110011101011101101100000101000000110010110011100001001000110001110100101010010101100011010110000010001011001111001111001110011111001100011110110001001101010000000000111101101010110101010011011011111110010111010000010001000101110101100111111111101110100100100000011110010010000110111100111010010101111010001111100011100100101010011111011100000000100100001001100110001000101000011010101010111110001000000011100100000001011011111111001001011000001100110101000000001100110000111000100110101110111101010100110001101001000010110011001011010000111111000111010101111100101100001011101100101000110001100001100010001001001110010000010001011111101101101111011101101100010110001000111001010101001011111011111100011001110001110010111000011010011101101000011100111011000001001010101010010001101011010101011001001010001000111100101000011000101011011110111011101001011001001011011001101111010010011110010011101011011010111001110110101010101010101010001100101000001000101101001010000101110101011011111101010001011111010111110011101111111010100000001100000001100100011001011110111011111110011101010111000011101100111111101100001111100001110100100001010011001110111010000011101110011010001111011110101010011010111011001000101111010000110000111010111110001101011010010100010111111100001011111000001010000001001101100110011001100001000101101001100101011101100011011000111100111000001001100001110100010110011101001111101011000100111010001111001111000110000101001000100010110110001011111001100110101110001110110101101010001010101110100100110100000101101000101010011000110101010101011110111011111110010111011111100000001000000011110101010100111110000010001000101000111011001100100011001101101010000101010001100000001010001110110000000000111100110000010101100001000101110101100010110000010101000110110100010110101001001010010001010111010001001100000000011101110110010111100000000010011001010010010111111010100001011010100101001001110101100000001011001100011100111110011001101111000001001101111100111010100111111101000011100100110111010100010010000001101111111100101111101000101101010000010010100000100111000000110101111101000101001010010001111101000000001011011000111110001111100111010000110000100000110010101110011010100110010010111111010101100101101001111001001000000000100011111000101101001011010110010100100000011001111100111101111111011010110010000111001000110000011001111111010010010100110000011100101011001000111011010101000001011100010001011001111001101000111011000110110001001110010000010111010110100100110000010011101001000100010010011100000010010110100100011011010111000110110110000001001011000000000100111000101110010110001111001001111001011011110010110010101010001100010101000100000110111010111001111000111000010000100101011001001100111100011111000001010110011110111000101000010000110011010110110101110100000101011011001011001110011001001101010001000000111110000101100001100101000110000011110001110000101001001100010101010000000100101100001101001111001001101000010100001011110011100000100000000100010101100111101100011101101010100010110000101010011011101011010100110111100110011001011001000110100001000100010111011011110111011011110100010010011100001011001111001010111000000010110110100001010111010000111110101000000110101100110001010001101110101001110100110110110011111100111101111111001010011011001101111000000110110111110111000010001011001011011010110000000010010100101000110110000100011011010011110100010101101101001001001100001100001011000110011011011110100100000010001011011111000010110010111010011110011110010000101111011100010010011101011110000101010010000011001110101111000001011110010111110001110110100101001001100000011001010010100011101001100110111000001011111011010000111100010111100000011000110001111010100000111000101100000011100100101111100101111100111011110100110011100011110100111010000010111000110100011001000010011100101111010010100110100100011101100111001100101111111110101101001010111100000110010000111100010100100110001010000010011101110111010110010100000110001111100001001000100001010001010110101110110110111100101110001110010010001010110000001000101010111110001100011111011100010110111011000000010101110111101100110000010000000010100111110011000010000001010101111010100000110001001011000010101111100001111110001101100011110110010010110011110110010001011100010010000000010000101010010111101001010101011001011110100000101111100111010010110010001110001010101000111001101110011100000010101101011000100010110011011011111100010110111110100111111101100001100000000000101000101011100111000000100110000010110110010001111000011000101110000010101110011011000010111110111110101101000000100000110101010001000100100000010000101010000011010001000001111001001010011101101001001100100101011111011010100101010001011110111110011101100001010111111110110111010000011111100001101101000010111010000110001111000101110010110110101110000100000111101001101100111100000110100011101101111011111010010001110010011011110101101100100001001000110100110110001010111011101101100101101000111001000110111101000000001011000100110100010011001000111110010101110010010011011011011001111110010000111011011101010001011011000011010111011100001011000111100101000011010101010010001000010000100001000101100111111111110000111110010011001000110110111010000000110110100100111101101011001111110011010100000111000100100110110000000001110110010000010100010100101001000001101101100101001111101000111100001111110011110001000110000011011100100110100000010111011011000000011000000001011101010110100111101001101100100001111000011101110011001111111011101011000100010011001101010100110000001100111000001011001010110100111001101111101101000100111110001111100001110010110000000100101011010111001001100011101110111011100111011111111111111010010101100111111100101011101101011011010111110011011110111011111100110101010000000000101111000010100100100000100001110011010011001001110111111110001101010001010000010110111111111011010000110101001010111111110110111000111011000101100001000011010111010010000111110110100110010111000011011100000110011000110100011111100110000101101111101001100101001010101001011111100010110110000100110111110010111100011010110000011001100111010010000111111011000010110100111111101000101101100110000001010011000011111101010000000010011101101101111000101100010100001100011011100001101111101011101010110001110000101011011100010100110011001100110001100001101001000000010101100001000000110101110111000110100100101000001110110111001000001101100011011000010101110011100001111000011101011001011010100001101101011111001000101001011101000010111011000101011101110110100011110111101110110011101111010111010111001001101110001011000100011101000111000001000000100100000101000100001001010101110111010010011000100000010000111101011000010100100111001111101111011001110101000000100001111111000111100100011101111111000001111111101100000110010001110110001111010100010000000010000010101010010101000001001000000011001101100001110111010001000101011001010000000100111101001111010010001011011101011011011101110100000110110001100001010010111000111010010011110010001001010001001000011110000001000001111001101001111011011100010110010010101000001011000000100000101000111110010001000100000000110111100001011100010011100001011100110100001000010101101010100001000000100100010101001000101110110111010111111000110000100010000011110100100111010100000011111101010110100110110100001000001001001010101011010101011110011111000011000000111101110100000100101111100111110110000100110100000011111110111110100001011010111010000100101010101110001100100101000101101101001011101010101100111001001101001000110101111111001111011110101011101001110100000010010001101101100000010110000011001100001110110010100000000011110111100000100001101011001100110110000001001000001101111001101101111001110011101101101010001101100110111110011110010001000110110010100100010100111000000100010111011100100110100100110111000110001010010000110000001101000010100011101110010110101100001101001101000000100100010000010000011110110101100000011110010101010010001100110101001100101000101100010101001010000111000111000000000001001100100110001111000110100001000000010001110011011001110000011001000011101010011110000101001101111100110000111110101111111111101011100101011001001010101101001100110110111000111110111011101000011001100101010111101100111010001001000001011001101101111110001001111111111110001000100011100101100110110101101100110110010110011111001101001011011111110001011110110000100010000101011110011001000100010100011101111100010011011010011100111110111000111100110000010011100111011100011001011001101101011001000001001010011101111010000101011010100011010011001111011010001111010010111001111000001000001111100100010110001000100011011110001010011101000110000100110010111110111110011000101001000111000001100001110010000011100010001111100011001110011100001100011010010000001000001100010100100110101001101110011001001010100011010110011111010101100111110010011100101101111001110001011001000101101100111010000100111101010101010100001100011001101011111111110010001111100111001001110010101100111001010000001000100000111101110111001110111100100110101000110000001100000101001010001111000001110110100000011100011011100011011100101010100110011111110110011110100100111100110110100111010100001011101010001000010011010011101010001010101000100011111110001010000000100100111101110100000100100110100000101001111011011011110011111101001011100010111110110100011001100000101000110100101101011001010111100000000111010101011101110101100100101001001111010001111100101010011011000101000111100010010010000001111001001100011110001000110011010110110100100111011100111010110111010110011010101010111100110000101101111101110010001010011000010111001110110000100100101010001001010110110001001001011001000010100111100010110110101000111100110010110001010010010000110010101111000010001100111010100010111100110000000010001101001011100110001011000100101000011101000010110100001110011010101111011101111110110011111111000111010010010100010011100100100100011111111011111110100110001100110\n" - ] - } - ], + "outputs": [], "source": [ "# A simple example.\n", "q = cirq.GridQubit(5, 2)\n", "circuit = cirq.Circuit(cirq.X(q)**0.5, cirq.measure(q, key='m'))\n", "\n", - "job = engine.run_sweep(\n", + "job = processor.run_sweep(\n", " program=circuit,\n", - " repetitions=10000,\n", - " processor_ids=['rainbow'],\n", - " gate_set=cirq_google.SYC_GATESET)\n", + " repetitions=1000)\n", "\n", "results = [str(int(b)) for b in job.results()[0].measurements['m'][:, 0]]\n", "print('Success! Results:')\n", diff --git a/docs/tutorials/google/start.ipynb b/docs/tutorials/google/start.ipynb index 636fb2fb6b6..889c763740e 100644 --- a/docs/tutorials/google/start.ipynb +++ b/docs/tutorials/google/start.ipynb @@ -11,20 +11,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "cellView": "form", "id": "_SAdH7I0B2rz" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", @@ -82,19 +74,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "id": "846b32703c5c" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "try:\n", " import cirq\n", @@ -147,31 +131,25 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { - "cellView": "form", "id": "YoqI9GrOPExP" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Notebook is not executed with Colab, assuming Application Default Credentials are setup.\n", - "Successful authentication to Google Cloud.\n" - ] - } - ], + "outputs": [], "source": [ "# The Google Cloud Project id to use.\n", "project_id = \"\" #@param {type:\"string\"}\n", - "processor_id = \"pacific\" #@param {type:\"string\"}\n", + "processor_id = \"\" #@param {type:\"string\"}\n", "\n", "from cirq_google.engine.qcs_notebook import get_qcs_objects_for_notebook\n", - "device_sampler = get_qcs_objects_for_notebook(project_id, processor_id)\n", - "\n", - "if not device_sampler.signed_in:\n", - " raise Exception(\"Please setup project_id in this cell or set the `GOOGLE_CLOUD_PROJECT` env var to your project id.\")" + "qcs_objects = get_qcs_objects_for_notebook(project_id, processor_id)\n", + "\n", + "project_id = qcs_objects.project_id\n", + "processor_id = qcs_objects.processor_id\n", + "engine = qcs_objects.engine\n", + "if not qcs_objects.signed_in:\n", + " print(\"ERROR: Please setup project_id in this cell or set the `GOOGLE_CLOUD_PROJECT` env var to your project id.\")\n", + " print(\"Using noisy simulator instead.\")\n" ] }, { @@ -182,7 +160,7 @@ "source": [ "**Authentication details** Double clicking on the project_id block above should expose the code that is run when you run this code block. This code uses the [colabtools](https://github.com/googlecolab/colabtools/blob/master/google/colab/auth.py) auth module to ensure that *Application Default Credentials* are set and then creates a variable `colab_auth` which can be used in Cirq to authenticate your calls to Quantum Computing Service.\n", "\n", - "If you are going to run code outside of colab and want to authenticate, see the below section on running from the command-line." + "If you are going to run code outside of colab and want to authenticate, see the below section on running from the command-line. (Note that this colab's automated outputs use a noisy simulator instead of authenticating to the production service)." ] }, { @@ -197,21 +175,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "cellView": "both", "id": "EQoTYZIEPa9S" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Circuit:\n", - "(0, 0): ───X───M('result')───\n" - ] - } - ], + "outputs": [], "source": [ "# Define a qubit at an arbitrary grid location.\n", "qubit = cirq.GridQubit(0, 0)\n", @@ -239,22 +208,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "id": "TW_zU_pagVP0" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Simulating circuit using Cirq...\n", - "\n", - "Measurement results:\n", - "result=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n" - ] - } - ], + "outputs": [], "source": [ "# Simulate the circuit, repeating 1000 times.\n", "print(\"Simulating circuit using Cirq...\\n\")\n", @@ -280,27 +238,17 @@ }, "source": [ "### Create a Quantum Engine client\n", - "Interactions with hardware are facilitated by the Quantum Computing Service. A client must first be initialized with your Google Cloud project to perform these interactions. " - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "id": "V40sPIi63f0I" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "# Create an Engine client.\n", - "engine = cg.get_engine()" + "Interactions with hardware are facilitated by the Quantum Computing Service. A client must first be initialized with your Google Cloud project to perform these interactions. \n", + "\n", + "There are two ways to create the engine client:\n", + "\n", + "`cg.Engine(project_id=YOUR_PROJECT_ID)`\n", + "\n", + "or you can use:\n", + "\n", + "`engine = cg.get_engine()`\n", + "\n", + "Note: for this tutorial, we already used `cg.engine.get_qcs_objects_for_notebook()` which defaults to a noisy simulator when credentials do not exist." ] }, { @@ -319,42 +267,7 @@ "metadata": { "id": "O-Jrib9y1TFY" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " (0, 5)───(0, 6)\n", - " │ │\n", - " │ │\n", - " (1, 5)───(1, 6)───(1, 7)\n", - " │ │ │\n", - " │ │ │\n", - " (2, 4)───(2, 5)───(2, 6)───(2, 7)───(2, 8)\n", - " │ │ │ │\n", - " │ │ │ │\n", - " (3, 5)───(3, 6)───(3, 7)───(3, 8)───(3, 9)\n", - " │ │ │\n", - " │ │ │\n", - " (4, 1)───(4, 2) (4, 4)───(4, 5)───(4, 6)───(4, 7)\n", - " │ │ │ │ │ │\n", - " │ │ │ │ │ │\n", - "(5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)───(5, 5)───(5, 6)───(5, 7)\n", - " │ │ │ │ │ │ │\n", - " │ │ │ │ │ │ │\n", - " (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)───(6, 6)───(6, 7)\n", - " │ │ │ │ │\n", - " │ │ │ │ │\n", - " (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6)\n", - " │ │ │\n", - " │ │ │\n", - " (8, 3)───(8, 4)───(8, 5)\n", - " │\n", - " │\n", - " (9, 4)\n" - ] - } - ], + "outputs": [], "source": [ "processor = engine.get_processor(processor_id)\n", "\n", @@ -376,21 +289,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "id": "ma8JDhTUR389" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(5, 2): ───PhXZ(a=-1,x=1,z=0)───M('result')───\n" - ] - } - ], + "outputs": [], "source": [ - "valid_qubit = device.qubits[0]\n", + "valid_qubit = sorted(device.metadata.qubit_set)[0]\n", "\n", "# Transform circuit to use an available hardware qubit.\n", "hw_circuit = circuit.transform_qubits(lambda q: valid_qubit)\n", @@ -413,35 +318,21 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "id": "-a0I0cbGyivS" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Uploading program and scheduling job on the Quantum Engine...\n", - "\n", - "Scheduled. View the job at: https://console.cloud.google.com/quantum/programs/prog-UEPDBU3CSSULX8IW200829?&project=quantum-cloud-client\n", - "Measurement results:\n", - "\n", - "1111111111111111101111111111111111110111101111111111111111111111111111111111111111111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111101111111111111111111111110111011111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111101111111111111111111111111111111111111111111110111111111101111111111111111111111111111111111111101111101111111111111111011111111111111111111011111111111111111111111110111101111111111111110111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111101111111111111101111111111111011111101111111111110111111011111111111111111111101111111111111111111111111111110111111111111111111111111111111110111111111111111011011111111111111111111111111111111111111111101111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111101111111111111111111110111111101111111111111111111111111111110111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111110011111111111111111111101111111111111111111111101011111111011010111111111111111111011110111111111111111111111111111011111111111111111011111011111111111111011111111111111111111111111111111111111111110110111111111111111111011111111111111111111111111111111111101111111111111111110111110111111101111111111111111101111111111111111110111111111111101111111111111110101111111111111111111111111111111111111111111111111111111001111111111111111111110111111111111111111111111111111110111011111111111111111111111111101111111111111111111111111111101111111110111110111111111111101111111111111111111111111111111101111111111111111111111111111110101111111111111111111111011111111111110111111111111111111111011111111111111111111111111110111111111111111111111111111111011111111111111111011111111111111111011111111110111111111111111111111011111111110111111111111111111111111111110011111111111111111111111111111111111111111101111111111111111111111111111111111101111111111111111111111111111111111111111111111010111111111111111111111111111111111111111111111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111101111111101111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111011111111111111111111111111111111101111111111111111111111110111111111111011111111111011101110111111111111101111111111111101111111011111010111111111110111111111111111111111101011111111111111111111111111111111111011111111111111111101111111111111111110111111011111111110011111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111101111111111101111111111111111111011110111111111111111110111110111110111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111101111111111011111111111111110111111111111111111111111111111111111111111111111101111111111111111111111111111111011111111111111111111111111110111111111111101001111111111111110111111111111111111111111111111111111111111111111111111101111111111111111111111111011111111111101111111111111111111111111111111111111111111111111111111110111111111011011111111111110111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111011111111111111111111111111111111110111111101111111111111111011111111111111111111111111111111111111111111111111111111111111111110111111111110111011111111111111111111111111111111111111111111111111111111111111110111111111111111111101111111111111111111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111011111100111111111101111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111001101110111111111101111111111111110111111111111111111111111110111111111111111111111111111111111011111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111011111111111111111111011111111111111111111111111111111101111011111111111111111111111101111111111111111111111011111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111011111111111110111111111110101111111111101111111111111111111111111111111111111111111111111110111110111111110111111111111111111111111111101101111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111011101111111101011111111111111011111011111111111111111110111011111111101111111111111111111111111111011111111111111111111111111111111111101111111111111110111111111111111111111101111111111111111111111110111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111101111111111111111111111111111111111110101111111011111111111111111111111111111111111111111111111101111111111111111111111011111101111111111111011111111111111111111111111111111111111111111111111111111111111101111111111111111101111111111111111111011111110111111111111110111111111111111111111111111111011110111111111111111111011111111111111111111111111111111101111111111111110111111100110111111111111111111111111111111111111111111111111111111111111111101011111111111111111111110111111110111111111111111111111111111111111111111111111111110111111111111111111111011111111111111011111101111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111101111111111111111111101111111111111111111111011111111101111111111111111111111111111111111111101111111010111111111111111111111111111111111111111101111111111111111111111011111111111111111111111111111111111111111111101111111111011111011111111111111111111111111111111111111111111110111111111101111111111111111111111111111111111111111111111111111111011111111111111111111111011111010111111111111111111111011111111111111111111111110111111111111111111111111111111111111111101111111111111100111111111011111111111111111111111101110111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111110111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111100111111111111111111111111111101011111111111111111111111111111111101111111111111111111111111111111110101111111111111111111111111110111111111111111111111111111111111011111111111111111111111111111110111111111111111111111111111111111111111010111111111111111111111111111011111111111111111111111111111011111111111111111111011111111111111111111111111111110111101111111111111111111111111111111111111111111111011111111111111111111111111111111111101111111111111111101110111111111111101111111111111111111111111111111111111111111111111111111111111110111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111101111111111111111111111111111111111011111101111111111111111111111111111111111111111111111111111101111111100111011111111111111111111111011111111011111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111101111111111111111111111011111111111111111111101111011111111111111111111111110111111111111111111111111111111111111111111111011110111111101111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111110111011111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111011111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111101101111111111101111101110111011111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111101111111111111111111111111111011011111111111111111111111111111101110111111111101111111110111111111111111111101111111111111111111111111111111111111111111111011111111111111101111111111111111111111111110111011111011111111111011111111111110111111111111111111111111111111111111111111111111111011111111111111111111011111101101111111111011111111111111011111111111111111111111111111111111111111111110111101111011111111111011111111111111111111111111011111011111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111101111111111111111110111111111111111111111100111111111111111111111111111111111011111111111110111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111110111111111011111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111110111111111111111111111111111111111111101111110111111111111101111111011111011111111111111111111110111111111111111011111111111111011011111111110111111111111111111111111111101101111011111111111111111111111111111111111111111101111111111111111111111101111111110011110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111101111111111111111111111111111111111111111111111111111111111111011111111111111111111111111011111111111111111111111111111111111101111111111111011111111111111111011101111111111\n" - ] - } - ], + "outputs": [], "source": [ "print(\"Uploading program and scheduling job on the Quantum Engine...\\n\")\n", "\n", "# Upload the program and submit jobs to run in one call.\n", - "job = engine.run_sweep(\n", + "job = processor.run_sweep(\n", " program=hw_circuit,\n", - " repetitions=10000,\n", - " processor_ids=[processor.processor_id])\n", + " repetitions=1000)\n", "\n", "print(\"Scheduled. View the job at: https://console.cloud.google.com/quantum/\"\n", - " \"programs/{}?&project={}\".format(job.program_id, project_id))\n", + " \"programs/{}?&project={}\".format(job.id(), project_id))\n", "\n", "# Print out the results. This blocks until the results are returned.\n", "results = job.results()\n", @@ -486,24 +377,16 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "id": "4RXHE070gleY" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "import cirq\n", "import cirq_google as cg\n", "\n", - "def example():\n", + "def example(engine, processor_id, project_id):\n", " \"\"\"Hello qubit example run against a quantum processor.\"\"\"\n", "\n", " # Define a qubit.\n", @@ -515,21 +398,16 @@ " cirq.measure(qubit, key='result') # Measurement.\n", " )\n", "\n", - " # Create an Engine object. This uses the project id of your\n", - " # Google cloud project.\n", - " project_id = 'your-project-id'\n", - " engine = cg.Engine(project_id=project_id)\n", - "\n", " print(\"Uploading program and scheduling job on Quantum Engine...\\n\")\n", "\n", + " processor = engine.get_processor(processor_id)\n", " # Upload the program and submit jobs to run in one call.\n", - " job = engine.run_sweep(\n", + " job = processor.run_sweep(\n", " program=circuit,\n", - " repetitions=1000,\n", - " processor_ids=[processor.processor_id])\n", + " repetitions=1000)\n", "\n", " print(\"Scheduled. View the job at: https://console.cloud.google.com/quantum/\"\n", - " f\"programs/{job.program_id}/jobs/{job.job_id}\"\n", + " f\"programs/{job.program().id()}/jobs/{job.id()}\"\n", " f\"/overview?project={project_id}\")\n", "\n", " # Print out the results. This blocks until the results are returned.\n", @@ -539,8 +417,23 @@ " print(result)\n", "\n", "\n", + "\n", "if __name__ == '__main__':\n", - " example()" + " virtual = False\n", + " # Needed for colab only. Can remove in stand-alone. \n", + " import os\n", + " if 'COLAB_GPU' in os.environ:\n", + " print(\"Running on Colab, using a virtual engine instead.\")\n", + " virtual = True\n", + " \n", + " # Set up QCS objects.\n", + " from cirq_google.engine.qcs_notebook import get_qcs_objects_for_notebook\n", + " qcs_objects = get_qcs_objects_for_notebook(virtual=True)\n", + " processor_id = qcs_objects.processor_id\n", + " project_id = qcs_objects.project_id\n", + " engine = qcs_objects.engine\n", + "\n", + " example(engine, processor_id, project_id)" ] }, { @@ -553,16 +446,7 @@ "\n", "`python hello_qubit.py`\n", "\n", - "The output should be something like:\n", - "\n", - "```\n", - "Uploading program and scheduling job on Quantum Engine...\n", - "\n", - "Scheduled. View the job at: https://console.cloud.google.com/quantum/programs/example-T5K9Y9/jobs/job-0?mods=quantum_ng2&project=quantum-cloud-client\n", - "\n", - "Measurement results:\n", - "1010101010111011000011101010101011010011100111110001101101101011100011111010100100011000010101110010011000100001110000100010011100111101001101101010000000111101010100001001101110101010110000010011110101100000101111101000010000000100010111010101110001110101101100010001101111100110001001010000100000110110100000111111110011100101111000010110010101100010111111100010001100111011010101111011000110100001010000011100000100101010111110111010011010100011011111110110011101010001000101000011101000111000001001101000110000111111000111100001001000010101011111000111111010100001011001010011011000010110111010001011000001011110001010110100001101011101110011111100100101010011100010010001100101100001101010010011001010100011010010011000010010001010111101001000011000101111011111100111111111101001010010001010000101011101100100001101011100101010101110111001101000100101111101100000100010000101000011110110101001101100000000000111000000111011101101110000001110110001111011000100111111101110111111101001110110000110\n", - "```\n" + "You should be able to see output like that shown above." ] }, {