Skip to content

Commit 99f6ede

Browse files
committed
Add showAnnotationRegions config option
1 parent 70747b7 commit 99f6ede

5 files changed

Lines changed: 42 additions & 7 deletions

File tree

src/crumpy/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class CircuitWidget(anywidget.AnyWidget):
4141
curveConnectors : bool
4242
If connectors (e.g., the line between control and target of a CNOT) can be drawn curved.
4343
Defaults to True (matching Crumble).
44+
showAnnotationRegions : bool
45+
If detector region and observable marks are shown.
46+
Defaults to True (matching Crumble).
4447
"""
4548

4649
_esm = bundler_output_dir / "bundle.js"
@@ -49,6 +52,7 @@ class CircuitWidget(anywidget.AnyWidget):
4952
)
5053
indentCircuitLines = traitlets.Bool(True).tag(sync=True)
5154
curveConnectors = traitlets.Bool(True).tag(sync=True)
55+
showAnnotationRegions = traitlets.Bool(True).tag(sync=True)
5256

5357
@staticmethod
5458
def from_cirq(cirq_circuit: cirq.Circuit):

src/js/crumble/draw/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const OFFSET_X = -pitch + Math.floor(pitch / 4) + 0.5;
2424
const OFFSET_Y = -pitch + Math.floor(pitch / 4) + 0.5;
2525
let indentCircuitLines = true;
2626
let curveConnectors = true;
27+
let showAnnotationRegions = true;
2728

2829
const setIndentCircuitLines = (newBool) => {
2930
if (typeof newBool !== "boolean") {
@@ -39,13 +40,22 @@ const setCurveConnectors = (newBool) => {
3940
curveConnectors = newBool;
4041
};
4142

43+
const setShowAnnotationRegions = (newBool) => {
44+
if (typeof newBool !== "boolean") {
45+
throw new TypeError(`Expected a boolean, but got ${typeof newBool}`);
46+
}
47+
showAnnotationRegions = newBool;
48+
};
49+
4250
export {
4351
pitch,
4452
rad,
4553
OFFSET_X,
4654
OFFSET_Y,
4755
indentCircuitLines,
4856
curveConnectors,
57+
showAnnotationRegions,
4958
setIndentCircuitLines,
5059
setCurveConnectors,
60+
setShowAnnotationRegions,
5161
};

src/js/crumble/draw/main_draw.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
* - Refactored for CrumPy
1919
*/
2020

21-
import { pitch, rad, OFFSET_X, OFFSET_Y } from "./config.js";
21+
import {
22+
pitch,
23+
rad,
24+
showAnnotationRegions,
25+
OFFSET_X,
26+
OFFSET_Y,
27+
} from "./config.js";
2228
import { marker_placement } from "../gates/gateset_markers.js";
2329
import { drawTimeline } from "./timeline_viewer.js";
2430
import { PropagatedPauliFrames } from "../circuit/propagated_pauli_frames.js";
@@ -275,11 +281,14 @@ function draw(ctx, snap) {
275281
batch_input,
276282
);
277283
let batch_index = 0;
278-
for (let mi = 0; mi < dets.length; mi++) {
279-
propagatedMarkerLayers.set(~mi, batch_output[batch_index++]);
280-
}
281-
for (let mi of obs.keys()) {
282-
propagatedMarkerLayers.set(~mi ^ (1 << 30), batch_output[batch_index++]);
284+
285+
if (showAnnotationRegions) {
286+
for (let mi = 0; mi < dets.length; mi++) {
287+
propagatedMarkerLayers.set(~mi, batch_output[batch_index++]);
288+
}
289+
for (let mi of obs.keys()) {
290+
propagatedMarkerLayers.set(~mi ^ (1 << 30), batch_output[batch_index++]);
291+
}
283292
}
284293

285294
drawTimeline(

src/js/crumble/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import { Circuit } from "./circuit/circuit.js";
2222
import { draw } from "./draw/main_draw.js";
2323
import { EditorState } from "./editor/editor_state.js";
24-
import { setIndentCircuitLines, setCurveConnectors } from "./draw/config.js";
24+
import {
25+
setIndentCircuitLines,
26+
setCurveConnectors,
27+
setShowAnnotationRegions,
28+
} from "./draw/config.js";
2529

2630
const CANVAS_W = 600;
2731
const CANVAS_H = 300;
@@ -48,6 +52,7 @@ function render({ model, el }) {
4852
getStim: () => model.get("stim"),
4953
getIndentCircuitLines: () => model.get("indentCircuitLines"),
5054
getCurveConnectors: () => model.get("curveConnectors"),
55+
getShowAnnotationRegions: () => model.get("showAnnotationRegions"),
5156
};
5257

5358
const canvas = initCanvas(el);
@@ -76,6 +81,10 @@ function render({ model, el }) {
7681
setCurveConnectors(traitlets.getCurveConnectors());
7782
editorState.force_redraw();
7883
});
84+
model.on("change:showAnnotationRegions", () => {
85+
setShowAnnotationRegions(traitlets.getShowAnnotationRegions());
86+
editorState.force_redraw();
87+
});
7988

8089
// Listeners on editor state that trigger redraws
8190
editorState.rev.changes().subscribe(() => {
@@ -90,6 +99,7 @@ function render({ model, el }) {
9099
// Configure initial settings and stim
91100
setIndentCircuitLines(traitlets.getIndentCircuitLines());
92101
setCurveConnectors(traitlets.getCurveConnectors());
102+
setShowAnnotationRegions(traitlets.getShowAnnotationRegions());
93103
commitStimCircuit(traitlets.getStim());
94104
}
95105
export default { render };

tests/test_widget.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ def test_has_traits():
3535
assert widg.has_trait("stim")
3636
assert widg.has_trait("indentCircuitLines")
3737
assert widg.has_trait("curveConnectors")
38+
assert widg.has_trait("showAnnotationRegions")
3839
assert widg.trait_metadata("stim", "sync")
3940
assert widg.trait_metadata("indentCircuitLines", "sync")
4041
assert widg.trait_metadata("curveConnectors", "sync")
42+
assert widg.trait_metadata("showAnnotationRegions", "sync")
4143

4244

4345
def test_use_case_dlink():

0 commit comments

Comments
 (0)