First Steps with LoQS¶
A rough analogy of pyGSTi \(\rightarrow\) LoQS concepts:
| Concept | pyGSTi | LoQS (physical level) | LoQS (logical level) |
|---|---|---|---|
| Series of operations to apply | Circuit | <backend>PhysicalCircuit |
InstructionStack |
| Labels for operations | Labels | N/A | InstructionLabel |
| Custom operations | OperationFactories | N/A | Instruction |
| "Standard" operations | *Op (e.g., FullCPTPOp) |
N/A | Helper functions to build "standard" Instructions |
| Mapping from labels to operations | *Model (e.g. ExplicitOpModel) |
<backend>NoiseModel |
QECCode/QECCodePatch |
| The "state" | *State (e.g. CPTPState) |
<backend>QuantumState |
Frame (quantum and classical) |
| Flag for how to store state | Evotype | N/A | GateRep/InstrumentRep |
| Record of states after every operator | N/A | N/A | History (list of Frames) |
Thing with the .run() |
Protocols (e.g. GST) | N/A | QuantumProgram |
Output of .run() |
ModelEstimateResults | N/A | ProgramResults |
| Quickstart for operation mapping object | Modelpack | N/A | Codepack |
With this in mind and the documentation available, let's dive straight in!
+++
Grab a codepack¶
Where most pyGSTi examples start with a modelpack, we'll start with a codepack. We currently have two choices: the 5-qubit code and the Steane (or [[7,1,3]] color code).
I'm going to use the Steane code here, but feel free to go through and try this with the 5Q code later for extra credit.
from loqs.codepacks import codepack_7_1_3_quantinuum2021 as codepack_Steane
# We can print the module's docstring to get more information on the codepack
# This is the same as what is in the documentation (with less nice formatting)
print(codepack_Steane.__doc__)
Codepacks offer two things: a QECCode and an ideal physical noise model. Let's extract those here.
In order to define our physical noise model, we must make a few choices: qubit labels, model backend, and gate/instrument/measurement representations.
# Let's check what our available backends are
from loqs.backends import get_available_backends
get_available_backends()
## MAKE SURE YOU HAVE pygsti_circuit, pygsti_model, AND qsim_state AVAILABLE
# If you don't, reinstall with loqs[pygsti,quantumsim] (or `-e ."[pygsti,quantumsim]"`) at least and check again
# Let's define qubits for a single Quantinuum-style Steane patch: 7 data qubits, 3 aux qubits
qubits = ["A0", "A1", "A2"] + [f"D{i}" for i in range(7)]
# Let's use a pyGSTi noise model
# LoQS Best Practice: Always import backends from loqs.backends, and not a further submodule
# This is because loqs.backends handles missing backends by doing lazy imports
from loqs.backends import PyGSTiNoiseModel
ideal_model = codepack_Steane.create_ideal_model(qubits=qubits, model_backend=PyGSTiNoiseModel)
The QECCode creation function also has several options. For the purposes of this tutorial, we'll just explicitly set the circuit backend as a reminder that it's there.
from loqs.backends import PyGSTiPhysicalCircuit
steane_code = codepack_Steane.create_qec_code(circuit_backend=PyGSTiPhysicalCircuit)
Before we get too far, let's see what Instructions are available to us:
#steane_code.instructions
# Or displayed more compactly:
list(steane_code.instructions.keys())
We see some |0> state preps, a few logical operations, a bunch of SE/QEC instructions, and then some measurements.
Setting up our first program¶
Let's start simple: Let's do a state preservation experiment!
In order to do this, we need to create an initial History and set up an InstructionStack.
from loqs.core import Frame, History, InstructionStack, PatchLayout
from loqs.backends import NumpyStatevectorQuantumState, QSimQuantumState
# Our starting frame just needs two things:
# the physical quantum system, and
init_state = NumpyStatevectorQuantumState(10, qubit_labels=qubits)
# letting LoQS know we want this to be a Steane code patch
init_patches = PatchLayout({
"L0": steane_code.create_patch(qubits=qubits)
})
# And now we bundle it up
init_frame = Frame({"state": init_state, "patches": init_patches})
init_history = History(init_frame)
# And finally, let's create our stack for a |0> state preservation with one round of QEC
# Each entry becomes an InstructionLabel, really just a dict:
# {"instruction": <key in steane_code.instructions>, "patch_label": <key in the Frame's patches>, **other kwargs}
# The (instruction, patch_label) tuple below is shorthand for that dict when
# there are no other kwargs -- see the "Building a Complex Instruction" tutorial.
stack = InstructionStack([
("FT Zero Prep", "L0"),
("Adaptive QEC", "L0"),
("FT Logical Z Measure", "L0")
])
And finally, we are ready to run some shots!
from loqs.core import QuantumProgram
program1 = QuantumProgram(
instruction_stack=stack,
initial_history=init_history,
default_noise_model=ideal_model,
name="Zero state preservation"
)
results1 = program1.run(num_shots=100)
Great, but how do we look at the results? We could print the full History for each shot, but the results object has some convenient collation tools.
There are some caveats: To use the collation tools effectively, you have to have some idea of what is in the Frames that you want to check. In our case, all LoQS codepacks currently include logical_measurement in measurement Instructions.
results1.collect_shot_data(
key="logical_measurement", # key in the Frame,
indices=-1, # Check the last frame of each shot,
return_counter=True # For convenience, just tell us how many of each output there were
)
Exercise 1¶
Can you do this for the |1>, |+>, and |-> states?¶
You may be interested in the QuantumProgram.from_quantum_program function, which lets you copy a program but override some of the parameters. For example:
program2 = QuantumProgram.from_quantum_program(program1, instruction_stack=new_stack)
This can be used to override almost anything in the program: noise model, base RNG, etc. Any fields not passed in are not overwritten (but deepcopied from the source program so that we don't have any copy/reference shenanigans).
Hint 1
Check what other instructions are available in the codepack that might let you access the other states.Hint 2
Make sure you are measuring in the right basis!Solution
We just need several different stacks: For |1>stack = InstructionStack([
("FT Zero Prep", "L0"),
("X", "L0"),
("Adaptive QEC", "L0"),
("FT Logical Z Measure", "L0")
])
stack = InstructionStack([
("FT Zero Prep", "L0"),
("H", "L0"),
("Adaptive QEC", "L0"),
("FT Logical X Measure", "L0")
])
stack = InstructionStack([
("FT Zero Prep", "L0"),
("X", "L0")
("H", "L0"),
("Adaptive QEC", "L0"),
("FT Logical X Measure", "L0")
])
Exercise 2¶
Create a version of the state preservation test for the 5Q code instead of the Steane code.¶
Be aware that not all codepacks have the same instructions! As a hint, the 5Q codepack has |-> and X as the basic prep/meas, and Flagged QEC is the top-level QEC instruction.
Solution
We need to make a few changes: First, import and use the 5Q codepack instead of the Steane:from loqs.codepacks import codepack_5_1_3_quantinuum2022 as codepack_5Q
# We only need 7 qubits: 5 data qubits, 2 aux qubits
qubits = ["A0", "A1"] + [f"D{i}" for i in range(5)]
ideal_model = codepack_5Q.create_ideal_model(qubits=qubits)
code_5Q = codepack_5Q.create_qec_code()
init_patches = PatchLayout({
"L0": code_5Q.create_patch(qubits=qubits)
})
stack = InstructionStack([
("FT Minus Prep", "L0"),
("Flagged QEC", "L0"),
("FT Logical X Measure", "L0")
])