|
1 | | -# crumpy |
| 1 | +<div align="center"> |
2 | 2 |
|
3 | | -[![Actions Status][actions-badge]][actions-link] |
4 | | -[![Documentation Status][rtd-badge]][rtd-link] |
| 3 | +# CrumPy |
5 | 4 |
|
6 | | -[![PyPI version][pypi-version]][pypi-link] |
7 | | -[![Conda-Forge][conda-badge]][conda-link] |
8 | | -[![PyPI platforms][pypi-platforms]][pypi-link] |
| 5 | +Visualize quantum circuits with error propagation in a Jupyter widget. |
9 | 6 |
|
10 | | -[![GitHub Discussion][github-discussions-badge]][github-discussions-link] |
| 7 | +<img src="./_static/images/bit_flip_in_jupyter.png" alt="jupyter notebook cell with quantum circuit diagram output" width=400> |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +This package builds off of the existing circuit visualizations of |
| 12 | +[Crumble](https://algassert.com/crumble), a stabilizer circuit editor and |
| 13 | +sub-project of the open-source stabilizer circuit project |
| 14 | +[Stim](https://github.com/quantumlib/Stim). |
| 15 | + |
| 16 | +</div> |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +**Requires:** Python 3.11+ |
| 23 | + |
| 24 | +```console |
| 25 | +pip install crumpy |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +CrumPy provides a convenient Jupyter widget that makes use of Crumble's ability |
| 31 | +to generate quantum circuit timeline visualizations with Pauli propagation from |
| 32 | +[Stim circuit specifications](https://github.com/quantumlib/Stim/blob/main/doc/file_format_stim_circuit.md#the-stim-circuit-file-format-stim). |
| 33 | + |
| 34 | +### Using `CircuitWidget` |
| 35 | + |
| 36 | +`CircuitWidget` takes a |
| 37 | +[Stim circuit specification](https://github.com/quantumlib/Stim/blob/main/doc/file_format_stim_circuit.md#the-stim-circuit-file-format-stim) |
| 38 | +in the form of a Python `str`. To convert from the official Stim package's |
| 39 | +`stim.Circuit`, use `str(stim_circuit)`. If coming from another circuit type, it |
| 40 | +is recommended to first convert to a `stim.Circuit` (e.g., with Stim's |
| 41 | +[`stimcirq`](https://github.com/quantumlib/Stim/tree/main/glue/cirq) package), |
| 42 | +then to `str`. Note that not all circuits may be convertible to Stim. |
| 43 | + |
| 44 | +```python |
| 45 | +from crumpy import CircuitWidget |
| 46 | + |
| 47 | +your_circuit = """ |
| 48 | +H 0 |
| 49 | +CNOT 0 1 |
| 50 | +""" |
| 51 | + |
| 52 | +circuit = CircuitWidget(stim=your_circuit) |
| 53 | +circuit |
| 54 | +``` |
| 55 | + |
| 56 | +<img src="./_static/images/circuit_epr_pair.png" alt="quantum circuit that creates EPR pair" width=175> |
| 57 | + |
| 58 | +### Propagating Paulis |
| 59 | + |
| 60 | +A useful feature of Crumble (and CrumPy) is the ability to propagate Paulis |
| 61 | +through a quantum circuit. Propagation is done automatically based on the |
| 62 | +specified circuit and Pauli markers. From the |
| 63 | +[Crumble docs](https://github.com/quantumlib/Stim/tree/main/glue/crumble#readme): |
| 64 | + |
| 65 | +> Propagating Paulis is done by placing markers to indicate where to add terms. |
| 66 | +> Each marker has a type (X, Y, or Z) and an index (0-9) indicating which |
| 67 | +> indexed Pauli product the marker is modifying. |
| 68 | +
|
| 69 | +Define a Pauli X, Y, or Z marker with the `#!pragma MARK_` instruction. Note |
| 70 | +that for compatibility with Stim, the '`#!pragma `' is included as `MARK_` is |
| 71 | +not considered a standard Stim instruction: |
| 72 | + |
| 73 | +```python |
| 74 | +z_error_on_qubit_2 = "#!pragma MARKZ(0) 2" |
| 75 | +``` |
| 76 | + |
| 77 | +#### Legend |
| 78 | + |
| 79 | +<img src="./_static/images/pauli_legend.svg" alt="jupyter notebook cell with quantum circuit diagram output" width=200> |
| 80 | + |
| 81 | +#### Example |
| 82 | + |
| 83 | +```python |
| 84 | +circuit_with_error = """ |
| 85 | +#!pragma MARKX(0) 0 |
| 86 | +TICK |
| 87 | +CNOT 0 1 |
| 88 | +TICK |
| 89 | +H 0 |
| 90 | +""" |
| 91 | + |
| 92 | +CircuitWidget(stim=circuit_with_error) |
| 93 | +``` |
| 94 | + |
| 95 | +<img src="./_static/images/circuit_with_marker.png" alt="quantum circuit with Pauli propagation markers" width=200> |
| 96 | + |
| 97 | +Notice how the single specified Pauli X marker propagates both through the |
| 98 | +control and across the connector of the CNOT, and gets transformed into a Pauli |
| 99 | +Z error when it encounters an H gate. |
| 100 | + |
| 101 | +## Local Development |
| 102 | + |
| 103 | +See the [contribution guidelines](.github/CONTRIBUTING.md) for a quick start |
| 104 | +guide and Python best practices. |
| 105 | + |
| 106 | +### Additional requirements |
| 107 | + |
| 108 | +[npm/Node.js](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) |
| 109 | +(for bundling JavaScript); versions 11+/22+ recommended |
| 110 | + |
| 111 | +### Project Layout |
| 112 | + |
| 113 | +CrumPy makes use of the widget creation tool |
| 114 | +[AnyWidget](https://anywidget.dev/). With AnyWidget, Python classes are able to |
| 115 | +take JavaScript and display custom widgets in Jupyter environments. CrumPy |
| 116 | +therefore includes both JavaScript and Python subparts: |
| 117 | + |
| 118 | +```text |
| 119 | +crumpy/ |
| 120 | +├── src/ |
| 121 | +│ ├── crumpy/ |
| 122 | +│ │ ├── __init__.py # Python code for CircuitWidget |
| 123 | +│ │ └── bundle.js # Bundled JavaScript will appear here |
| 124 | +│ └── js/ |
| 125 | +│ ├── crumble/ # Modified Crumble code |
| 126 | +│ │ ├── main.js # Main circuit visualization/setup |
| 127 | +│ │ └── ... |
| 128 | +│ └── package.json # Bundling setup and scripts |
| 129 | +├── tests/ # Python tests |
| 130 | +└── ... |
| 131 | +``` |
| 132 | + |
| 133 | +`src/crumpy` contains the main Python package code. |
| 134 | + |
| 135 | +`src/crumpy/__init__.py` contains the main class of the `crumpy` package, |
| 136 | +`CircuitWidget`. |
| 137 | + |
| 138 | +`src/js/crumble/` contains the modified Crumble circuit visualization code that |
| 139 | +will be rendered in the `CircuitWidget` widget. |
| 140 | + |
| 141 | +`src/js/crumble/main.js` contains the main circuit visualization and setup logic |
| 142 | +in the form of the `render` function |
| 143 | +[used by AnyWidget](https://anywidget.dev/en/afm/#anywidget-front-end-module-afm). |
| 144 | + |
| 145 | +### Bundling |
| 146 | + |
| 147 | +To create a Jupyter widget, AnyWidget takes in JavaScript as an ECMAScript |
| 148 | +Module (ESM). **Any changes made to the JavaScript code that backs the circuit |
| 149 | +visualization will require re-bundling** into one optimized ESM file. |
| 150 | + |
| 151 | +To bundle the JavaScript: |
| 152 | + |
| 153 | +1. Navigate to `src/js` |
| 154 | +2. If you haven't yet, run `npm install` (this will install the |
| 155 | + [esbuild](https://esbuild.github.io/) bundler) |
| 156 | +3. Run `npm run build` (or `npm run watch` for watch mode) |
| 157 | + |
| 158 | +A new bundle should appear at `src/crumpy/bundle.js`. |
| 159 | + |
| 160 | +**Note**: If you are working in a Jupyter notebook and re-bundle the JavaScript, |
| 161 | +you may need to restart the notebook kernel and rerun any widget-displaying code |
| 162 | +for the changes to take effect. |
11 | 163 |
|
12 | 164 | <!-- SPHINX-START --> |
13 | 165 |
|
|
0 commit comments