![]() |
![]() |
![]() |
![]() |
This notebook includes a couple of clean and succinct code blocks that you can build on or copy and paste elsewhere in order to make use of the Quantum Virtual Machine without worrying about how it works inside.
Install Cirq and qsim
Setup
# @title Install `cirq_google` and `qsimcirq`
try:
import cirq
import cirq_google
except ImportError:
print("installing cirq...")
!pip install --upgrade --quiet cirq-google~=1.0.dev
print("installed cirq.")
import cirq
import cirq_google
try:
import qsimcirq
except ImportError:
print("installing qsimcirq...")
!pip install --quiet qsimcirq
print(f"installed qsimcirq.")
import qsimcirq
import time
Create a Quantum Virtual Machine.
Instantiate a cirq.SimulatedLocalEngine
that uses the Virtual Engine Interface.
# @title Choose a processor ("willow_pink" or "rainbow" or "weber")
# (see cirq_google.engine.list_virtual_processors() for available names)
processor_id = "willow_pink" # @param {type:"string"}
# Instantiate an engine.
sim_engine = cirq_google.engine.create_default_noisy_quantum_virtual_machine(
processor_id=processor_id, simulator_class=qsimcirq.QSimSimulator
)
print(
"Your quantum virtual machine",
processor_id,
"is ready, here is the qubit grid:",
"\n========================\n",
)
print(sim_engine.get_processor(processor_id).get_device())
Your quantum virtual machine willow_pink is ready, here is the qubit grid: ======================== (0, 6)────(0, 7)────(0, 8) │ │ │ │ │ │ (1, 5)────(1, 6)────(1, 7)────(1, 8) │ │ │ │ │ │ │ │ (2, 4)────(2, 5)────(2, 6)────(2, 7)────(2, 8)────(2, 9)────(2, 10) │ │ │ │ │ │ │ │ │ │ │ │ │ │ (3, 3)───(3, 4)────(3, 5)────(3, 6)────(3, 7)────(3, 8)────(3, 9)────(3, 10) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (4, 2)───(4, 3)───(4, 4)────(4, 5)────(4, 6)────(4, 7)────(4, 8)────(4, 9)────(4, 10)────(4, 11)───(4, 12) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (5, 1)───(5, 2)───(5, 3)───(5, 4)────(5, 5)────(5, 6)────(5, 7)────(5, 8)────(5, 9)────(5, 10)────(5, 11)───(5, 12) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (6, 0)───(6, 1)───(6, 2)───(6, 3)───(6, 4)────(6, 5)────(6, 6)────(6, 7)────(6, 8)────(6, 9)────(6, 10)────(6, 11)───(6, 12)───(6, 13)───(6, 14) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (7, 2)───(7, 3)───(7, 4)────(7, 5)────(7, 6)────(7, 7)────(7, 8)────(7, 9)────(7, 10)────(7, 11)───(7, 12)───(7, 13) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (8, 2)───(8, 3)───(8, 4)────(8, 5)────(8, 6)────(8, 7)────(8, 8)────(8, 9)────(8, 10)────(8, 11)───(8, 12) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (9, 4)────(9, 5)────(9, 6)────(9, 7)────(9, 8)────(9, 9)────(9, 10)────(9, 11) │ │ │ │ │ │ │ │ │ │ │ │ │ │ (10, 4)───(10, 5)───(10, 6)───(10, 7)───(10, 8)───(10, 9)───(10, 10) │ │ │ │ │ │ │ │ (11, 6)───(11, 7)───(11, 8)───(11, 9) │ │ │ │ │ │ (12, 6)───(12, 7)───(12, 8)
Create a device-ready circuit.
To learn how to create a device ready circuit, have a look at the QVM Circuit Preparation page.
# create your device ready circuit here!
q0 = cirq.GridQubit(6, 3)
your_circuit = cirq.Circuit([(cirq.X**0.5)(q0), cirq.measure(q0)])
print(your_circuit)
(6, 3): ───X^0.5───M───
Execute Your circuit on the Quantum Virtual Machine.
# @title Enter the name of your device ready circuit and execute it on the Quantum Virtual Machine
circuit = your_circuit # @param
reps = 3000
start = time.time()
results = sim_engine.get_sampler(processor_id).run(circuit, repetitions=reps)
elapsed = time.time() - start
print('Circuit successfully executed on your quantum virtual machine', processor_id)
print(f'QVM runtime: {elapsed:.04g}s ({reps} reps)')
print('You can now print or plot "results"')
Circuit successfully executed on your quantum virtual machine willow_pink QVM runtime: 0.07114s (3000 reps) You can now print or plot "results"
print(f"measurements: {results.histogram(key=q0)}")
measurements: Counter({1: 1527, 0: 1473})