How to Run a State Vector Simulation¶
This how-to guide walks you through running state vector simulation for a simple Workbench program.
Workbench state vector simulator allows you to simulate small (~30 qubits) programs. This is the default simulator in Workbench; when you create a QPU without specifying the list of filters to use, it will use state vector simulator to execute the program. You can read more about the state vector simulator in Simulating Workbench Programs.
Step 1. Initialize the QPU¶
Create a QPU object. Do not provide any arguments beyond num_qubits to the constructor.
qpu = QPU(num_qubits=4)
Alternatively, if you need to specify the list of filters, use SV_DEFAULT filter preset or make sure to include the '>>state-vector-sim>>' filter in the list.
qpu = QPU(num_qubits=4, filters=['>>buffer>>', '>>state-vector-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 state 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 from How to Write a Simple Program that prepares two Bell states and then measures them. 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 workbench_algorithms import ArbitraryStatePrep
# Initialize a QPU using the state vector simulator (the default setup)
qpu = QPU(num_qubits=4)
# Allocate qubits
reg1 = Qubits(2, "reg1", qpu=qpu)
reg2 = Qubits(2, "reg2", qpu=qpu)
# Perform the computation on one register
reg1[0].had()
reg1[1].x(cond=reg1[0])
# Print the state of the QPU
qpu.print_state_vector()
# Perform the computation on the other register
state_prep = ArbitraryStatePrep([1, 0, 0, 1])
state_prep.compute(reg2)
# Print the state of the QPU
qpu.print_state_vector()
# Get the results from the simulator
res1 = reg1.read()
res2 = reg2.read()
print("Measurement results:", res1, res2)
# Print the state of the QPU after the measurements
_ = qpu.print_state_vector()
|reg1|reg2> |0|0> 0.707107+0.000000j |3|0> 0.707107+0.000000j |reg1|reg2> |0|0> 0.500000+0.000000j |0|3> 0.500000+0.000000j |3|0> 0.500000+0.000000j |3|3> 0.500000+0.000000j Measurement results: 3 0 |reg1|reg2> |3|0> 1.000000+0.000000j
You can read more about debugging tools offered by the state vector simulator in the Testing and Debugging tutorial.