View on QuantumAI | Run in Google Colab | View source on GitHub | Download notebook |
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
# @title Install `cirq_google` and `qsimcirq`
try:
import cirq
import cirq_google
except ImportError:
print("installing cirq...")
!pip install --quiet cirq-google
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 ("rainbow" or "weber")
processor_id = "rainbow" # @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 rainbow is ready, here is the qubit grid: ======================== (3, 2) │ │ (4, 1)───(4, 2)───(4, 3) │ │ │ │ │ │ (5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4) │ │ │ │ │ │ │ │ (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5) │ │ │ │ │ │ │ │ (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6) │ │ │ │ │ │ (8, 3)───(8, 4)───(8, 5) │ │ (9, 4)
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(4, 1)
your_circuit = cirq.Circuit([(cirq.X**0.5)(q0), cirq.measure(q0)])
print(your_circuit)
(4, 1): ───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 rainbow QVM runtime: 0.03729s (3000 reps) You can now print or plot "results"