Skip to content

Commit

Permalink
Add pre-commit hooks (QCHackers#291)
Browse files Browse the repository at this point in the history
Fixes QCHackers#288 by introducing `pre-commit` that should run on each commit
AND on CI.
  • Loading branch information
nelimee authored Aug 7, 2024
1 parent c5f2e43 commit fa50fad
Show file tree
Hide file tree
Showing 119 changed files with 617 additions and 406 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Asking a question
about: I have a question to ask that is neither a feature request nor a bug report.
title: "[Question]: "
labels: "question"
assignees:
assignees:
- nelimee
---

Expand Down
28 changes: 23 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ on:
- main

jobs:
pre-commit-ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt -r requirements-dev.txt
- name: Run pre-commit hooks
run: pre-commit run --all-files

build-and-test-python:
runs-on: ubuntu-latest
needs: pre-commit-ci
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
Expand All @@ -25,24 +41,26 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt
python -m pip install -r requirements.txt -r requirements-dev.txt
- name: Analyze code with pylint
run: |
pip install pylint
python -m pip install pylint
pylint $(git ls-files '*.py')
continue-on-error: true
- name: Build
run: pip install .
- name: Test and coverage
run: |
pip install pytest pytest-cov
python -m pip install pytest pytest-cov
pytest --cov=src/tqec $(git ls-files '*_test.py')
- name: Mypy type checking
run: |
pip install mypy
python -m pip install mypy
mypy src/tqec/
build-and-test-nodejs:
runs-on: ubuntu-latest
needs: pre-commit-ci
defaults:
run:
working-directory: frontend
Expand Down
34 changes: 34 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.6
hooks:
# Run the linter.
- id: ruff
types_or: [python, pyi, jupyter]
args: [--fix]
# Run the formatter.
- id: ruff-format
types_or: [python, pyi, jupyter]
- repo: https://github.com/crate-ci/typos
rev: v1.23.6
hooks:
- id: typos
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4
hooks:
- id: gitleaks
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Design automation software tools for Topological Quantum Error Correction

Live endpoint: https://tqec.app
Live endpoint: [https://tqec.app](https://tqec.app)

## Installation

Expand Down Expand Up @@ -40,12 +40,12 @@ The next step is to install the package and its dependencies using
python -m pip install "."
```

If you want to run the
[`logical_qubit_memory_experiment.ipynb`](https://github.com/QCHackers/tqec/blob/main/notebooks/logical_qubit_memory_experiment.ipynb)
notebook, you will also have to install a few other packages:
If you want to help developing the `tqec` Python package, you should install the development
environment by using instead

```sh
python -m pip install ".[dev]"
pre-commit install
```

If no errors happen, the package is installed! You can try to launch a jupyter
Expand Down
5 changes: 5 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[default]
extend-ignore-identifiers-re = [
# Corrected to "and" but always mean "ancilla".
"anc_.*",
]
82 changes: 41 additions & 41 deletions docs/detectors.rst
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
Automatic detector computation
==============================

The ``tqec`` package implements a method to automatically compute detectors from a
The ``tqec`` package implements a method to automatically compute detectors from a
given quantum circuit representing a quantum error corrected computation.

An accompanying notebook showcasing the different steps to find detectors is located
An accompanying notebook showcasing the different steps to find detectors is located
`here <media/detectors/detector_computation_illustration.ipynb>`_.

Difference from the main ``tqec`` package
Difference from the main ``tqec`` package
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The implementation to automatically find detectors in a given quantum circuit representing
a quantum error corrected computation is currently kept as isolated from the ``tqec`` package
as possible and will very likely be outsourced into its own independent package in the future.
a quantum error corrected computation is currently kept as isolated from the ``tqec`` package
as possible and will very likely be outsourced into its own independent package in the future.
The implementation can be found in ``tqec.circuit.detectors`` and a few restrictions applies
specifically to the code in this sub-module:

- it should be self-contained, or in other words it should not call code from the main
``tqec`` package, with the exception of code within ``tqec.circuit.detectors``,
- it should avoid uneeded dependencies. For that reason, the code in ``tqec.circuit.detectors``
does not use ``cirq`` as its intermediary representation but directly operates on
- it should avoid unneeded dependencies. For that reason, the code in ``tqec.circuit.detectors``
does not use ``cirq`` as its intermediary representation but directly operates on
``stim`` data-structures.

Concepts used through the sub-module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A few core concepts are re-used through the whole sub-module. This section aims at
A few core concepts are re-used through the whole sub-module. This section aims at
presenting these core concepts in an accessible manner.

Terms that are conventionally used in quantum computing, such as "circuit", will not
be defined here.
Terms that are conventionally used in quantum computing, such as "circuit", will not
be defined here.

.. _moments-section:

Moments
^^^^^^^

The concept of "moment" is central to the sub-module. A "moment" is a portion of a
The concept of "moment" is central to the sub-module. A "moment" is a portion of a
quantum circuit that is located between two ``TICK`` instructions. Conventionally,
moments may end with a ``TICK`` instruction, but do not start with one.

This definition is close, but not equivalent, to the definition used by ``cirq``. One of
the main difference is that ``cirq`` defines a moment as a sub-circuit of depth 1: no
instruction in the moment overlaps with another instruction in the same moment.
In the definition given above, there is no notion of depth, and even though ``TICK``
instruction are generally used in ``stim`` such as the moment definition above
This definition is close, but not equivalent, to the definition used by ``cirq``. One of
the main difference is that ``cirq`` defines a moment as a sub-circuit of depth 1: no
instruction in the moment overlaps with another instruction in the same moment.
In the definition given above, there is no notion of depth, and even though ``TICK``
instruction are generally used in ``stim`` such as the moment definition above
is equivalent to the definition used by ``cirq``, this is not required.

For example, the circuit
For example, the circuit

.. code-block::
Expand All @@ -72,20 +72,20 @@ Fragments

Fragments are a collection of moments that check the following order:

1. zero or more moments exclusively composed of `reset`, annotation or
1. zero or more moments exclusively composed of `reset`, annotation or
noisy-gate instructions,
2. zero or more moments composed of "computation" instructions (anything
2. zero or more moments composed of "computation" instructions (anything
that is not a measurement, a reset, a noisy gate or an annotation),
3. one or more moments exclusively composed of `measurement`, annotation
3. one or more moments exclusively composed of `measurement`, annotation
or noisy-gate instructions.

The circuit provided in :ref:`moments-section` contains 4 moments that form
a fragment.

Note that the circuit
Note that the circuit

.. code-block::
M 1 3
DETECTOR(1, 0) rec[-2]
DETECTOR(3, 0) rec[-1]
Expand All @@ -94,9 +94,9 @@ Note that the circuit
DETECTOR(3, 1) rec[-1] rec[-2] rec[-4]
OBSERVABLE_INCLUDE(0) rec[-1]
also form a fragment!
also form a fragment!

Flows
Flows
^^^^^

Stabilizers can propagate through a fragment, but can also be created or destroyed by it.
Expand All @@ -115,53 +115,53 @@ Let's use the following quantum circuit to illustrate:
You can also `check it interactively using crumble <https://algassert.com/crumble#circuit=Q(0,0)0;Q(1,0)1;Q(2,0)2;Q(3,0)3;Q(4,0)4;R_1_3;TICK;CX_0_1_2_3;TICK;CX_2_1_4_3;TICK;M_1_3;DT(1,0,0)rec[-2];DT(3,0,0)rec[-1]_rec[-2];DT(3,0,1)rec[-1]/>`_.

This quantum circuit can "propagate" a stabilizer, for example if a ``Z`` stabilizer
This quantum circuit can "propagate" a stabilizer, for example if a ``Z`` stabilizer
on qubit ``2`` is given as input, a ``Z`` stabilizer on qubit ``2`` is propagated
through:

.. image:: media/detectors/stabilizer-propagation.png

The quantum circuit also "creates" propagation of stabilizers with its resets, as
The quantum circuit also "creates" propagation of stabilizers with its resets, as
shown in the illustration below:

.. image:: media/detectors/stabilizer-creation.png

Here, the fragment is creating a ``Z0Z2`` stabilizer: the ``Z`` Pauli string
comes out of the final moment of the fragment on qubits ``0`` and ``2``.
Here, the fragment is creating a ``Z0Z2`` stabilizer: the ``Z`` Pauli string
comes out of the final moment of the fragment on qubits ``0`` and ``2``.

Quantum circuit can also "destroy" some incomming stabilizers with its measurements
Quantum circuit can also "destroy" some incoming stabilizers with its measurements
as shown below:

.. image:: media/detectors/stabilizer-destruction.png

Here, the fragment is destroying an incoming ``Z0Z2`` stabilizer.

These three kinds of stabilizer propagation are "flows". A "flow" is describing the
These three kinds of stabilizer propagation are "flows". A "flow" is describing the
way a stabilizer propagates through a fragment.

Collapsing operations
^^^^^^^^^^^^^^^^^^^^^

Collapsing operations are operations that are involved in the collapsing
(i.e., "destruction") or "creation" of flows.
Collapsing operations are operations that are involved in the collapsing
(i.e., "destruction") or "creation" of flows.

Measurements and resets are the only collapsing operations.

Note that, by construction, collapsing operations can only be encountered at the
Note that, by construction, collapsing operations can only be encountered at the
boundaries (beginning/end) of a fragment.

Boundary stabilizers
Boundary stabilizers
^^^^^^^^^^^^^^^^^^^^

An important data-structure used in the package is ``BoundaryStabilizer``. This
data-structure represents the state of a flow at the boundaries of a fragment.
An important data-structure used in the package is ``BoundaryStabilizer``. This
data-structure represents the state of a flow at the boundaries of a fragment.
It stores:

1. a Pauli string, representing the state of a flow that propagated from one boundary,
just before encountering the collapsing operations of the other boundary,
2. a list of Pauli strings, each representing one of the collapsing operations that
will be encountered by the propagated Pauli string,
3. a list of measurements that are involved (either as sources for a destruction flow
3. a list of measurements that are involved (either as sources for a destruction flow
or as sinks for a creation flow) in the flow propagation and that will be used later
to know which measurements to include in the detectors.

Expand All @@ -173,18 +173,18 @@ can be represented by a ``BoundaryStabilizer`` instance with the following attri

1. ``Z0Z1Z2`` for the stabilizer, as before encountering the measurement, the propagated
stabilizer is ``Z0Z1Z2``.
2. ``[Z1, Z3]`` as collapsing operations, as the measurements present in the above circuit
2. ``[Z1, Z3]`` as collapsing operations, as the measurements present in the above circuit
are performed in the ``Z`` basis, and on qubits ``1`` and ``3``.
3. a data-structure that will represent the measurement performed on qubit ``1``, as the
3. a data-structure that will represent the measurement performed on qubit ``1``, as the
only measurement that is taking part in the stabilizer propagation is this one.

As another example, the following flow:

.. image:: media/detectors/stabilizer-destruction.png

can be represented by a ``BoundaryStabilizer`` instance that has exactly the same attributes
can be represented by a ``BoundaryStabilizer`` instance that has exactly the same attributes
as the one described above.

To distinguish between these two ``BoundaryStabilizer`` (one representing a creation flow,
To distinguish between these two ``BoundaryStabilizer`` (one representing a creation flow,
the other representing a destruction flow), they will be stored in a data-structure that
will differentiate creation and destruction flows: ``FragmentFlow`` (or ``FragmentLoopFlow``).
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ TQEC
.. toctree::
:maxdepth: 2
:hidden:

Introduction <self>
Automatic detector computation <detectors>

.. toctree::
:maxdepth: 2
:caption: Notebooks:
Automatic detector computation <media/detectors/detector_computation_illustration>

Automatic detector computation <media/detectors/detector_computation_illustration>
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
"\n",
"In such cases, there is an intermediary step `1.5` that consist in try to merge several anti-commuting flows together to form new commuting flows.\n",
"\n",
"Comming back to the code, detectors can be listed by calling `match_detectors_from_flows_shallow`:"
"Coming back to the code, detectors can be listed by calling `match_detectors_from_flows_shallow`:"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion frontend/.gcloudignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
.gitignore

# Node.js dependencies:
node_modules/
node_modules/
2 changes: 1 addition & 1 deletion frontend/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions frontend/src/plaquettes/Footprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export default class Footprint {

/**
* Update the visibility and event listeners
* @function updateVisiblityAndEventListeners
* @function updateVisibilityAndEventListeners
* @returns {void}
*/
updateVisiblityAndEventListeners() {
updateVisibilityAndEventListeners() {
this.isDragging = true;
this.container.name = 'footprint';
// Create the buttons
Expand Down
Loading

0 comments on commit fa50fad

Please sign in to comment.