"full" completeness tier can be compiled to (see
Completeness tiers).
haiqu.qec_compile fetches the current data of the code from the library and raises
ValueError for a code in another tier.
Problems that are found during the compilation, such as a gate the code cannot implement or an
invalid compilation option, do not raise an error in the call to haiqu.qec_compile. They make the
job fail, and the job’s result() raises
JobFailedError, whose message describes the problem and, for a problem in one
circuit, names the circuit.
A compilation job that has not finished after 30 minutes is cancelled, and result then raises
JobCancelledError.
The compilation is controlled by three options, given to haiqu.qec_compile as keyword arguments:
ec_cycle_frequency(int, default1) sets where error-correction cycles are inserted, as explained in Error-correction cycles.variant_selections(dict orNone, defaultNone) selects which of the implementations declared by the code is used for each operation, as explained in Variant selections.add_layer_barriers(bool, defaultFalse): whenTrue, a barrier is placed at every boundary between consecutive logical layers of the compiled circuit. These barriers do not count as layers, so the error-correction cycles stay where they are.
compilation_options of the job and of the qec field of each compiled
circuit.
Error-correction cycles
An error-correction cycle extracts the syndrome of the code blocks, which is what the decoder uses to detect and correct errors when the compiled circuit is run. Theec_cycle_frequency option
sets where the cycles are inserted.
Layers. The circuit is first translated into the native logical gates of the code (see
Supported logical operations).
The translated circuit is then divided into layers as early as possible: each instruction is placed
in the first layer after all instructions it depends on, that is, all earlier instructions that act
on one of its qubits or classical bits.
Frequency. With ec_cycle_frequency=N, one error-correction cycle is inserted after every
N layers that contain a logical gate. The default is 1, a cycle after every such layer, and
0 inserts no cycles at all. The value must be a non-negative integer; any other value makes the
job fail.
- The identity gate
idcounts as a logical gate, soidgates can be used as explicit memory steps: threeidgates withec_cycle_frequency=1give three cycles. - Layers that contain only measurements, resets or barriers do not count.
- No cycle is inserted directly after the state preparation at the start of the circuit, and no
cycle is forced before the final measurement: layers after the last full group of
Nlayers get no cycle. - The layers are counted after the translation into native gates, so a gate that is decomposed into several native gates can contribute several layers.
- Each cycle covers every code block whose data qubits have not yet been measured. Currently, a
block that has been measured gets no further cycles, also after a
reset.
h(0); h(0); measure(0, 0) from the example above:
Variant. A code can declare several variants of its error-correction cycle, which differ in how
the syndrome is extracted, for example in a single round or in repeated rounds. A variant is selected
with
variant_selections={"error_correction_cycle": "<name>"}, as explained in
Variant selections.
Variant selections
A code’s.qecx file can declare several implementations, called variants, of each of the code’s
operations, for example several circuits that prepare a logical state, and marks one variant of each
operation as its default. The variant_selections option selects variants by name. It is a
dictionary with up to seven keys. Every key that is left out, and every operation that a given key
does not name, uses the code’s default variant; None or an empty dictionary selects the default
everywhere. Which variants a code declares, and their names, is defined by the code’s .qecx file
and shown in the code library on the Haiqu dashboard.
For each logical gate and logical measurement, a code’s file declares one or more operand groups:
the operand shapes the operation can act on, each with its own variants. An entry selects the
variant of one operand group. Its
"operands" list has one item per operand position, in order,
and must be written exactly as the operand group is declared in the file. Each item is a dictionary
with the keys "block" and "logical_qubit":
"block"labels the code block that the position acts on. Positions on the same block carry the same label and positions on different blocks carry different labels, so a gate between two code blocks has two different labels, for example0and1."logical_qubit"is the index of the logical qubit within that block, orNonefor a position that matches any logical qubit of the block.
error_correction_cycle has no variant 'bogus'. A key of
variant_selections other than the seven above, or a value of the wrong form, also makes the
job fail.
With the circuit from the example at the top of this page, the first example selects a variant of
the error-correction cycle, and the second the variant of the h gate:
Supported logical operations
Each qubit of the circuit is a logical qubit. For a code with one logical qubit, each qubit of the circuit is encoded in its own code block. For a code with several logical qubits, the size of each quantum register of the circuit must be a positive multiple of that number; other sizes make the job fail. For example, a two-qubit circuit compiled tocolor_488_d3, a code with one logical
qubit, is encoded in two code blocks:
- State preparation. Every logical qubit starts in the logical state
|0>. - Gates. The circuit is translated into the native logical gates of the code. A gate that the
code does not implement natively, but that can be expressed exactly in its native gates, is
decomposed into them. A gate that cannot be expressed in them, for example the non-Clifford gate
tfor a code without a non-Clifford native gate, makes the job fail with the message “failed to translate the circuit to the code’s basis gates”. - Measurement.
measurereads all data qubits of the qubit’s code block in the Z basis. The circuit must contain at least one measurement; a circuit without one makes the job fail. Running a circuit that uses a qubit again after measuring it is subject to the current limitations of qec_run. - Reset.
resetreturns a logical qubit to the logical state|0>. - Barriers are accepted.
- Classical registers. A circuit can have several classical registers.
.qecx file.
A circuit with unbound parameters, or with delay or box instructions, cannot be compiled and
makes the job fail.
Which logical gates a code implements natively, and how, is defined by the code’s .qecx file and
shown in the code library on the Haiqu dashboard.
Custom codes
Besides the codes of the library,code accepts a code of your own, defined in a .qecx file in
the QEC Exchange Format (QECX), plain or ZIP-archived. The file can be given in three forms:
- a path, as a
pathlib.Pathor a string, for example"codes/my_code.qecx"; - a file opened in binary mode,
open(path, "rb"), which is read from its current position and left open; - an in-memory
io.BytesIO.
TypeError, because reading a QECX file as text can
corrupt it. A string is read as a path when it names an existing file or contains / or \,
and as the slug of a library code otherwise. So a file in the working directory named exactly like a
library code’s slug is used instead of that code; pass the code returned by haiqu.get_qec_code to
select the library code in that case.
my_code-3f9a1c2e; for a file object without a
name, such as an io.BytesIO, it starts with custom_code. A file may be at most 5 MB;
a larger one raises requests.HTTPError (status 413). An empty file raises ValueError.
The file is neither checked against the code library nor validated in the call to
haiqu.qec_compile. A file that is malformed, that does not implement the operations of the
circuit, or whose operations do not match what the file declares about them makes the job fail, and
the job’s result() raises
JobFailedError, whose message describes the problem. The last case can
affect files that are otherwise valid, for example a file in which a measurement that is declared to
read out one operator measures a different one. The completeness tiers of the library do not apply to
custom codes.
The compiled circuits record the custom code they were compiled to, so they are run with exactly that
version of the file, also after the file is edited (see
Custom codes in QEC runs).
After an update of the Haiqu cloud, compiled circuits, including those compiled to a custom code, may
need to be compiled again (see QEC Run).
Haiqu.qec_compile(circuits, code, job_name=None, job_description=None, **compilation_options)
Compile logical circuits to a code of the Haiqu QEC code library or to a custom code. Each circuit is encoded into the chosen code, and error-correction cycles are inserted between its logical gates, as explained in Error-correction cycles. The operations a circuit can contain are listed in Supported logical operations. The compilation runs asynchronously on the Haiqu cloud as a job, which produces one compiled circuit for each given circuit, ready to be passed tohaiqu.qec_run.
Problems that are found during the compilation, such as a gate the code cannot implement or an
invalid compilation option, do not raise an error in this call. They make the job fail, and the
job’s result() raises
JobFailedError, whose message describes the problem.
- Parameters:
- circuits (QuantumCircuit | list *[*QuantumCircuit ] | CircuitModel | list *[*CircuitModel ]) — The logical circuit to compile, or a list of them. Each qubit of a circuit is a logical qubit. For a code with one logical qubit, each qubit of the circuit is encoded in its own code block. For a code with several logical qubits, the size of each quantum register of the circuit must be a positive multiple of that number, and other sizes make the job fail.
-
code (str | QECCodeModel | os.PathLike | BinaryIO) —
The code to compile to, in one of these
forms:
- A code of the Haiqu QEC code library, given by its slug, e.g.
"color_488_d3", or as a code returned byhaiqu.get_qec_codeorhaiqu.list_qec_codes. Only codes in the"full"completeness tier can be compiled to (see Completeness tiers). The current data of the code is always fetched from the library, also when a code model is given. - A custom code, given as the path to its QECX file, as a
pathlib.Pathor a string, e.g."codes/my_code.qecx". - A custom code, given as a file opened in binary mode,
open(path, "rb"), or as anio.BytesIO. It is read from the current position of the file, which is left open. A file opened in text mode is refused, because reading a QECX file as text can corrupt it.
/or\, and as a slug otherwise. So a file in the working directory named exactly like the slug of a library code is used instead of that code; pass the code model to select the library code in that case. A custom code belongs to the current experiment and is identified by its content: the same content is stored once, however often and under whatever file name it is given, and an edited file becomes a new custom code. A QECX file may be at most 5 MB. It is neither checked against the code library nor validated in this call, so a file that is malformed or cannot be compiled to makes the job fail. This includes well-formed files whose operations do not match what the file declares about them, for example a measurement that is declared to read out an operator but measures a different one. The compiled circuits record their code, sohaiqu.qec_runneeds nocodefor them, and it uses exactly the version of the file they were compiled with, also after the file is edited. - A code of the Haiqu QEC code library, given by its slug, e.g.
-
job_name (str | None) — Name of the compilation job. Defaults to
None, no name. -
job_description (str | None) — Description of the compilation job. Defaults to
None, no description. -
**compilation_options —
Options of the compilation, given as keyword arguments:
ec_cycle_frequency(int): A non-negative integerN: an error-correction cycle is inserted after everyNlayers that contain a logical gate, and0inserts no cycles. Defaults to 1. See Error-correction cycles.variant_selections(dict | None): The variants, alternative implementations declared by the code, to use for the code’s operations, given by name, e.g.{"error_correction_cycle": "<name>"}. Defaults toNone, the code’s default variant for every operation. See Variant selections.add_layer_barriers(bool): Whether to place a barrier at every boundary between consecutive logical layers of the compiled circuit. Defaults toFalse.
- Returns:
The compilation job. Its
result()waits for the job to finish and returns the compiled circuits. - Return type: QECCompilationJobModel
- Raises:
- ValueError — If a library code is not in the
"full"tier, or a custom code’s file is empty. A slug that names no code makes the job fail. - FileNotFoundError — If
codeis a path, or a string containing/or\, that does not exist. - TypeError — If
codeis a file opened in text mode, or of a type not listed above. - requests.HTTPError — If a custom code’s QECX file is larger than 5 MB (status 413), or a request
fails for another reason. In a notebook, an error message is shown and
Noneis returned instead.
- ValueError — If a library code is not in the
Examples
code:
class haiqu.sdk.schemas.QECCompilationJobModel
Job that compiles logical circuits to a QEC code, as returned byhaiqu.qec_compile.
Call result() to wait for the job to finish and obtain the compiled circuits.
id
ID of the job, for retrieving it later withhaiqu.get_job.
- Type: str
status
Status of the job when it was last checked.- Type: JobStatus
circuit_ids
IDs of the submitted logical circuits.- Type: list
code_slug
Slug of the target QEC code, as given incode.
- Type: str
compilation_options
The compilation options exactly as given tohaiqu.qec_compile, {} when none were given.
- Type: dict
compiled_circuit_ids
IDs of the compiled circuits, available once the job has finished,None until then.
- Type: list | None
progress()
Display a progress widget for the job and stream its log messages.result()
Wait for the job to finish and return the compiled circuits. The call blocks until the job has finished, checking its status every few seconds, with no timeout. Interrupting the wait (for example with Ctrl+C) returnsNone without
cancelling the job; call result() again to resume waiting. The job can also be
retrieved later by its ID with haiqu.get_job.
Each compiled circuit carries a qec field (see QECCircuitInfoModel) and can
be passed to haiqu.qec_run.
- Returns:
The compiled circuit when the job produced
one, otherwise a list of the compiled circuits.
Noneif the wait was interrupted. - Return type: CircuitModel | list[CircuitModel] | None
- Raises:
- haiqu.sdk.exceptions.JobFailedError — If the job failed. The message describes the problem,
and the exception’s
logsholds all log messages of the job. - haiqu.sdk.exceptions.JobCancelledError — If the job was cancelled, by the user or because it exceeded the time limit of 30 minutes, which the message then says.
- haiqu.sdk.exceptions.JobFailedError — If the job failed. The message describes the problem,
and the exception’s
class haiqu.sdk.schemas.QECCircuitInfoModel
QEC compilation information carried by a QEC-compiled circuit. The compiled circuits returned by a QEC compilation job hold this model in theirqec
field, which marks them as ready to be passed to haiqu.qec_run.
code_slug
Slug of the QEC code the circuit was compiled to.- Type: str
compilation_options
The compilation options exactly as given to the compilation job,{} when none were given.
- Type: dict