Quickstart

This page loads a QECX file, validates it, edits it and saves it again. It uses one of the example files distributed with the package, so you need no file of your own. Install the package first (see Installation).

Load a document

QECXDocument.load() reads a QECX file into a QECXDocument, the typed, editable model of the file’s content:

from qecx import QECXDocument, examples

path = examples.path("steane_7_1_3.qecx")
doc = QECXDocument.load(path)

print(doc.code_name, doc.code_type)                       # Steane Code css
print(doc.code_data.n, doc.code_data.k, doc.code_data.d)  # 7 1 3

load takes a path or a readable binary stream, and reads plain-text JSON and ZIP archives alike: it tells them apart by content. It validates the file first and raises QECXValidationFailedError if any finding is an error; warning findings are emitted as Python warnings. With validate=False, a file that breaks the format’s cross-field rules still loads, so you can inspect and repair it; a file that cannot be built into a document, such as one with a reference that does not resolve, still raises QECXLoadError.

To look at the whole document, doc.view().summary() prints a summary of every section. In Jupyter, a document displays as an HTML summary with circuit diagrams, given the viewer extra.

Validate

validate_qecx_file() validates a file on disk and returns every finding, errors and warnings alike, instead of raising. Each finding is a ValidationError with a severity, the path of the offending field, a message and a rule ID:

from qecx import validate_qecx_file

for finding in validate_qecx_file(path):
    print(f"[{finding.severity}] {finding.path}: {finding.message} ({finding.rule_id})")

The example file is valid, so this prints nothing. To see a finding, validate a broken copy of its content with validate_qecx(), which takes the parsed JSON as a dict:

import json
from qecx import validate_qecx

data = json.loads(path.read_text())
data["code"]["H_X"][0] = [1, 1, 0, 0, 0, 0, 0]  # an X check that anticommutes with a Z check

for finding in validate_qecx(data):
    print(finding.severity, finding.rule_id, finding.path)  # error CSS_ORTHOGONALITY code

A document checks itself the same way: QECXDocument.validate() returns the findings for its current content. The Format requirements section of the format specification lists everything a valid file must meet.

Edit

The example declares two error-correction cycles. This makes the repeated one the default and updates the file’s description:

cycles = doc.error_correction_cycles
print(list(cycles.variants), cycles.default.name)  # ['single_sequential', 'repeated_sequential'] single_sequential

doc.set_default(cycles.variants["repeated_sequential"])
doc.update_properties(description="Steane code, with d rounds of syndrome extraction.")

print(doc.error_correction_cycles.default.name)  # repeated_sequential

The model objects inside a document are frozen. An edit replaces them in the document and updates everything that refers to them, so read them from the document again after an edit: cycles above still shows the old default. Each edit checks the objects it touches, and one that fails raises QECXAuthoringError and leaves the document unchanged. The full set of rules runs only when the document is loaded, validated or saved.

Save

QECXDocument.save() writes the document back. By default each part of a loaded document is written where it sat, so a document loaded from plain-text JSON saves as plain-text JSON again. force_archive=True writes a ZIP archive instead:

doc.save("steane_edited.qecx")                        # plain-text JSON
doc.save("steane_archived.qecx", force_archive=True)  # ZIP archive

print(QECXDocument.load("steane_edited.qecx").error_correction_cycles.default.name)
# repeated_sequential

Both files take the .qecx extension; force_archive and the layout argument choose the form. Saving validates the document first and raises QECXValidationFailedError instead of writing an invalid file. The saved file declares the format version the package writes.

To look inside an archive or edit one by hand, see ZIP archive container.

Next steps

  • Command line: the qecx command, to summarize, inspect and validate a file from the terminal.

  • Examples: a complete QECX file explained key by key, and the other example files, with a CSS and a stabilizer Steane code, color codes and a rotated surface code.

  • Format specification: the normative definition of everything a QECX file holds.