How to Write a Simple Program¶
This how-to guide walks you through the high-level steps of writing a simple Workbench program.
You can read more about each step of the process in the corresponding tutorial.
Step 1. Initialize a QPU¶
A Workbench program starts by creating a QPU object - the representation of the quantum processing unit on which this program will run. This object specifies how the program will be compiled and executed.
qpu = QPU(num_qubits=4)
Step 2. Allocate qubits¶
The next step is allocating the qubits for the program to use. Workbench represents qubit registers as either "raw" qubits (Qubits data type) or arithmetic data types.
reg1 = Qubits(2, "reg1", qpu=qpu)
reg2 = Qubits(2, "reg2", qpu=qpu)
Step 3. Perform the computation¶
The next step is implementing the logic of the computation the program needs to do. In Workbench, there are three main ways of doing this:
For simple programs, you can apply primitive gates directly using the methods of the Qubits data type.
reg1[0].had() reg1[1].x(cond=reg1[0])
For more complicated logic, you can use built-in library routines in Workbench and in Workbench Algorithms ⧉.
state_prep = ArbitraryStatePrep([1, 0, 0, 1]) state_prep.compute(reg2)
You can also write your own routines to implement custom logic.
Step 4. Get the results¶
The final step is getting the results using measurements.
res1 = reg1.read()
res2 = reg2.read()
Example¶
The following example shows a Workbench program that prepares two Bell states in two different ways (using the primitive gates and using state preparation library) and then measures them.
from psiqworkbench import QPU, Qubits
from workbench_algorithms import ArbitraryStatePrep
# Initialize a QPU
qpu = QPU(num_qubits=4)
# Allocate qubits
reg1 = Qubits(2, "reg1", qpu=qpu)
reg2 = Qubits(2, "reg2", qpu=qpu)
# Perform the computation using primitive gates
reg1[0].had()
reg1[1].x(cond=reg1[0])
# Perform the computation using library routines
state_prep = ArbitraryStatePrep([1, 0, 0, 1])
state_prep.compute(reg2)
# Get the results
res1 = reg1.read()
res2 = reg2.read()
print("Measurement results:", res1, res2)
Measurement results: 3 0