Skip to main content
QEC compilation encodes a logical circuit into a quantum error-correcting code from the Haiqu QEC code library, or into a code of your own given as a QECX file (see Custom codes). Each logical qubit of the circuit is encoded in a code block of physical qubits, every logical operation is replaced by its implementation in the code, and error-correction cycles are inserted between the logical gates. The compilation runs asynchronously on the Haiqu cloud as a job, and the result of the job is the compiled circuit, which can then be run with qec_run.
Only codes in the "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, default 1) sets where error-correction cycles are inserted, as explained in Error-correction cycles.
  • variant_selections (dict or None, default None) selects which of the implementations declared by the code is used for each operation, as explained in Variant selections.
  • add_layer_barriers (bool, default False): when True, 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.
A value of the wrong type makes the job fail. Other keys are not supported. The options are stored, exactly as given, in 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. The ec_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 id counts as a logical gate, so id gates can be used as explicit memory steps: three id gates with ec_cycle_frequency=1 give 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 N layers 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.
For the circuit 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 example 0 and 1.
  • "logical_qubit" is the index of the logical qubit within that block, or None for a position that matches any logical qubit of the block.
A name, key or operand group that the code does not declare makes the job fail, with a message that names it, for example 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 to color_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 t for 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. measure reads 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. reset returns a logical qubit to the logical state |0>.
  • Barriers are accepted.
  • Classical registers. A circuit can have several classical registers.
How state preparation and reset are implemented, including whether they are fault-tolerant, is defined by the code’s .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.Path or 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.
A file opened in text mode is refused with 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.
A custom code is stored with 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 with its own slug. The slug is the file name without its extension, in lowercase, followed by a random suffix, for example 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 to haiqu.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 by haiqu.get_qec_code or haiqu.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.Path or a string, e.g. "codes/my_code.qecx".
      • A custom code, given as a file opened in binary mode, open(path, "rb"), or as an io.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.
      A string is read as a path when it names an existing file or contains / 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, so haiqu.qec_run needs no code for them, and it uses exactly the version of the file they were compiled with, also after the file is edited.
    • 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 integer N: an error-correction cycle is inserted after every N layers that contain a logical gate, and 0 inserts 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 to None, 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 to False.
      A value of the wrong type makes the job fail. Other keys are not supported.
  • 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 code is a path, or a string containing / or \, that does not exist.
    • TypeError — If code is 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 None is returned instead.

Examples

Compile a two-qubit circuit to a code with one logical qubit, which encodes each qubit in its own code block:
Compile to a custom code from a QECX file, then run the compiled circuit without code:

class haiqu.sdk.schemas.QECCompilationJobModel

Job that compiles logical circuits to a QEC code, as returned by haiqu.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 with haiqu.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 in code.
  • Type: str

compilation_options

The compilation options exactly as given to haiqu.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) returns None 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. None if the wait was interrupted.
  • Return type: CircuitModel | list[CircuitModel] | None

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 their qec 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