How to Run a "Classical" Simulation¶
This how-to guide walks you through running simulation of a reversible computation - a quantum computation that implements a classical computation, such as quantum arithmetic - in Workbench.
Workbench bit vector simulator allows you to simulate programs that include only a subset of quantum operations: the X gate, its controlled variants, and measurements. The limited set of gates means that the program state is always a basis state and thus can be simulated efficiently. You can read more about the bit vector simulator in Simulating Workbench Programs.
Step 1. Initialize the QPU¶
Create a QPU object using the BIT_DEFAULT filter preset.
from psiqworkbench.filter_presets import BIT_DEFAULT
qpu = QPU(num_qubits=100, filters=BIT_DEFAULT)
Alternatively, if you need to specify the list of filters, make sure to include the '>>bit-sim>>' filter in the list.
qpu = QPU(num_qubits=100, filters=['>>buffer>>', '>>bit-sim>>'])
Step 2. Write the program¶
Write the Workbench code following the rest of the steps outlined in How to Write a Simple Program.
Step 3. Execute the program¶
Now, when you run this Workbench program as Python code, either as a script or via Jupyter Notebooks, the QPU will send the instructions to the bit vector simulator and retrieve the information (either measurement outcomes or debugging information) from it.
Example¶
The following example shows how to get measurement results generated by the Workbench program that initializes a quantum register with a classical value, adds another classical value to it, and then measures it. It also shows how to get debugging information, such as the state vector at any point of the program, from the QPU.
from psiqworkbench import QPU, Qubits
from psiqworkbench.filter_presets import BIT_DEFAULT
# Initialize a QPU using the bit vector simulator
qpu = QPU(num_qubits=100, filters=BIT_DEFAULT)
# Allocate qubits
reg = Qubits(100, "reg", qpu=qpu)
# Initialize the register
reg.write(12345678901234567890)
# Perform the computation
reg += 10
# Print the state of the QPU
qpu.print_state_vector()
# Get the results from the bit simulator
print("Measurement result:", reg.read())
|reg> |12345678901234567900> 1.000000+0.000000j Measurement result: 12345678901234567900