Skip to content

Commit e4f140e

Browse files
committed
Update starter notebook with circuit import methods
1 parent 99f6ede commit e4f140e

3 files changed

Lines changed: 291 additions & 2232 deletions

File tree

docs/notebooks/getting_started.ipynb

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,10 @@
177177
"metadata": {},
178178
"outputs": [],
179179
"source": [
180-
"widg.indentCircuitLines = False\n",
181180
"widg.curveConnectors = False\n",
182181
"\n",
183-
"# Can also specify at construction:\n",
184-
"# CircuitWidget(stim=..., indentCircuitLines=False, curveConnectors=False)"
182+
"# Can also specify at construction, e.g.:\n",
183+
"# CircuitWidget(stim=..., curveConnectors=False)"
185184
]
186185
},
187186
{
@@ -276,16 +275,6 @@
276275
"For example, consider the circuit specification below which omits the '`#!pragma `' preceding its `MARKZ` instruction. Attempting to create a `stim.Circuit` will cause an error:"
277276
]
278277
},
279-
{
280-
"cell_type": "code",
281-
"execution_count": null,
282-
"id": "4aa33573",
283-
"metadata": {},
284-
"outputs": [],
285-
"source": [
286-
"%pip install stim"
287-
]
288-
},
289278
{
290279
"cell_type": "code",
291280
"execution_count": null,
@@ -367,16 +356,6 @@
367356
"The `traitlets` package used behind the scenes of both `CircuitWidget` and the widgets of `ipywidgets` allows for the composition of multiple widgets through functions like `link` and `dlink`. See the `ipywidgets` [documentation](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#linking-widgets) for more advanced uses."
368357
]
369358
},
370-
{
371-
"cell_type": "code",
372-
"execution_count": null,
373-
"id": "e27207a6",
374-
"metadata": {},
375-
"outputs": [],
376-
"source": [
377-
"%pip install ipywidgets"
378-
]
379-
},
380359
{
381360
"cell_type": "markdown",
382361
"id": "d3f8883a-12d8-4594-aec3-e7b98873ebe2",
@@ -423,25 +402,36 @@
423402
"id": "ed2c23e0",
424403
"metadata": {},
425404
"source": [
426-
"## Appendix: Converting to Stim"
405+
"### Converting Directly from a Circuit (`stim`, `cirq`, `qiskit`)"
427406
]
428407
},
429408
{
430409
"cell_type": "markdown",
431410
"id": "ed4ffb4e",
432411
"metadata": {},
433412
"source": [
434-
"If `stim` isn't your quantum circuit package of choice, it may still be possible to utilize CrumPy. Circuits from packages like `cirq` or `qiskit` may be convertible to a `stim` circuit (which can be easily converted to a string for use with `CircuitWidget`). For circuit languages that don't have an existing transpiler that converts to Stim, it may also be possible to convert to [OpenQASM](https://en.wikipedia.org/wiki/OpenQASM), which—while maybe not the most efficient—can be [converted to Cirq](https://quantumai.google/cirq/build/interop#importing_from_openqasm) and finally [converted to Stim](https://github.com/quantumlib/Stim/tree/main/glue/cirq#readme).\n",
413+
"CrumPy provides helper methods for creating a `CircuitWidget` directly from 3 popular quantum circuit packages: `stim`, `cirq`, and `qiskit`.\n",
435414
"\n",
436-
"Note that it may not always be possible to convert a circuit between formats (notably, `stim` has a [limited gate set](https://github.com/quantumlib/Stim/tree/main?tab=readme-ov-file#what-is-stim:~:text=There%20is%20no%20support%20for%20non%2DClifford%20operations%2C%20such%20as%20T%20gates%20and%20Toffoli%20gates.%20Only%20stabilizer%20operations%20are%20supported.))."
415+
"Even if `stim` isn't your quantum circuit package of choice, it may still be possible to utilize CrumPy. Circuits from packages like `cirq` or `qiskit` may be convertible to a `stim` circuit. For circuit languages that don't have an existing transpiler that converts directly to Stim, it may also be possible to convert to [OpenQASM](https://en.wikipedia.org/wiki/OpenQASM), which can be [converted to Cirq](https://quantumai.google/cirq/build/interop#importing_from_openqasm) and finally [converted to Stim](https://github.com/quantumlib/Stim/tree/main/glue/cirq#readme).\n",
416+
"\n",
417+
"Note that it may not always be possible to convert a circuit between formats (notably, `stim` has a [limited gate set](https://github.com/quantumlib/Stim/tree/main?tab=readme-ov-file#what-is-stim:~:text=There%20is%20no%20support%20for%20non%2DClifford%20operations%2C%20such%20as%20T%20gates%20and%20Toffoli%20gates.%20Only%20stabilizer%20operations%20are%20supported.)).\n",
418+
"\n"
437419
]
438420
},
439421
{
440422
"cell_type": "markdown",
441423
"id": "b4dd3937",
442424
"metadata": {},
443425
"source": [
444-
"### Example: `cirq` to `stim` with `stimcirq`"
426+
"#### Examples"
427+
]
428+
},
429+
{
430+
"cell_type": "markdown",
431+
"id": "30d3991d",
432+
"metadata": {},
433+
"source": [
434+
"`CircuitWidget.from_stim`"
445435
]
446436
},
447437
{
@@ -451,79 +441,76 @@
451441
"metadata": {},
452442
"outputs": [],
453443
"source": [
454-
"%pip install cirq\n",
455-
"%pip install stimcirq"
444+
"import stim\n",
445+
"\n",
446+
"stim_circuit = stim.Circuit(\"\"\"\n",
447+
"Y 0\n",
448+
"CY 0 1 \n",
449+
"\"\"\")\n",
450+
"\n",
451+
"CircuitWidget.from_stim(stim_circuit)"
456452
]
457453
},
458454
{
459455
"cell_type": "markdown",
460-
"id": "7ad3a4cc",
456+
"id": "d2ee059f",
461457
"metadata": {},
462458
"source": [
463-
"Let's create a simple `cirq` circuit we want converted to `stim` (note that importing cirq might take a minute):"
459+
"`CircuitWidget.from_cirq`"
464460
]
465461
},
466462
{
467463
"cell_type": "code",
468464
"execution_count": null,
469-
"id": "b089b365",
465+
"id": "0649f636",
470466
"metadata": {},
471467
"outputs": [],
472468
"source": [
473469
"import cirq\n",
474470
"\n",
475471
"q0 = cirq.LineQubit(0)\n",
476472
"q1 = cirq.LineQubit(1)\n",
477-
"cirq_circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1))\n",
473+
"cirq_circuit = cirq.Circuit(cirq.X(q0), cirq.CNOT(q0, q1))\n",
478474
"\n",
479-
"cirq_circuit"
475+
"CircuitWidget.from_cirq(cirq_circuit)"
480476
]
481477
},
482478
{
483479
"cell_type": "markdown",
484-
"id": "ad52fa36",
480+
"id": "ff13af42",
485481
"metadata": {},
486482
"source": [
487-
"We can use the `stimcirq` package (a sub-package within the Stim project) to convert to a `stim.Circuit`:"
483+
"`CircuitWidget.from_qiskit`"
488484
]
489485
},
490486
{
491487
"cell_type": "code",
492488
"execution_count": null,
493-
"id": "6f24253b",
489+
"id": "8bf6aa0b",
494490
"metadata": {},
495491
"outputs": [],
496492
"source": [
497-
"import stimcirq\n",
493+
"import qiskit\n",
494+
"\n",
495+
"qiskit_circuit = qiskit.QuantumCircuit(2)\n",
496+
"qiskit_circuit.z(0)\n",
497+
"qiskit_circuit.cz(0, 1)\n",
498498
"\n",
499-
"converted_stim = stimcirq.cirq_circuit_to_stim_circuit(cirq_circuit)\n",
500-
"converted_stim"
499+
"CircuitWidget.from_qiskit(qiskit_circuit)"
501500
]
502501
},
503502
{
504503
"cell_type": "markdown",
505-
"id": "9e736aee",
504+
"id": "b3b2651f",
506505
"metadata": {},
507506
"source": [
508-
"Note that for `CircuitWidget` compatibility, we need this circuit as a string:"
509-
]
510-
},
511-
{
512-
"cell_type": "code",
513-
"execution_count": null,
514-
"id": "432fe474",
515-
"metadata": {},
516-
"outputs": [],
517-
"source": [
518-
"converted_stim_str = str(converted_stim)\n",
519-
"\n",
520-
"CircuitWidget(stim=converted_stim_str)"
507+
"While it's not possible to directly add Pauli markers or other Crumble-specific instructions when creating `CircuitWidget`s with these methods, it is possible to read/write the resulting `CircuitWidget`'s `.stim` attribute to get the final converted Stim circuit (in string form), which can then be modified."
521508
]
522509
}
523510
],
524511
"metadata": {
525512
"kernelspec": {
526-
"display_name": "Python 3 (ipykernel)",
513+
"display_name": "crumpy",
527514
"language": "python",
528515
"name": "python3"
529516
},

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dynamic = ["version"]
2929
dependencies = [
3030
"anywidget>=0.9.18",
3131
"cirq>=1.3.0",
32+
"ipywidgets>=8.1.7",
3233
"ply>=3.11",
3334
"qiskit>=2.1.2",
3435
"stim>=1.15.0",
@@ -47,7 +48,6 @@ Changelog = "https://github.com/Deltakit/crumpy/releases"
4748
test = [
4849
"pytest >=6",
4950
"pytest-cov >=3",
50-
"ipywidgets>=8.1.7",
5151
]
5252
dev = [
5353
{ include-group = "test" },

0 commit comments

Comments
 (0)