# 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](installation.md)). ```{py:currentmodule} qecx ``` ## Load a document {py:meth}`QECXDocument.load` reads a QECX file into a {py:class}`QECXDocument`, the typed, editable model of the file's content: ```python 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 {py:class}`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 {py:class}`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 {py:func}`validate_qecx_file` validates a file on disk and returns every finding, errors and warnings alike, instead of raising. Each finding is a {py:class}`ValidationError` with a severity, the path of the offending field, a message and a rule ID: ```python 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 {py:func}`validate_qecx`, which takes the parsed JSON as a dict: ```python 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: {py:meth}`QECXDocument.validate` returns the findings for its current content. The [Format requirements](format_specification.md#8-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: ```python 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 {py:class}`QECXAuthoringError` and leaves the document unchanged. The full set of rules runs only when the document is loaded, validated or saved. ## Save {py:meth}`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: ```python 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 {py:class}`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 {ref}`ZIP archive container `. ## Next steps - [Command line](command_line.md): the `qecx` command, to summarize, inspect and validate a file from the terminal. - [Examples](examples.md): 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](format_specification.md): the normative definition of everything a QECX file holds.