Skip to content

Commit 7516f80

Browse files
authored
Merge pull request #41 from qsharp-community/quantikz-scope
added scope grouping for quantikz
2 parents c394730 + ce0e858 commit 7516f80

9 files changed

Lines changed: 208 additions & 81 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "qsharp-bridge"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2024"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

examples/python/jupyter/quantikz.ipynb

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"First, import the library.\n",
88
"\n",
9-
"You will also need to have LaTeX installed on your system for rendering the diagrams. We also import official Q# library as we will use it for comparison of the generated diagrams."
9+
"We will also need to have LaTeX installed on your system for rendering the diagrams. We also import official Q# library as we will use it for comparison of the generated diagrams."
1010
]
1111
},
1212
{
@@ -29,7 +29,7 @@
2929
"cell_type": "markdown",
3030
"metadata": {},
3131
"source": [
32-
"Next, load the Jupyter TikZ extension. "
32+
"Next, let's load the Jupyter TikZ extension. "
3333
]
3434
},
3535
{
@@ -45,7 +45,7 @@
4545
"cell_type": "markdown",
4646
"metadata": {},
4747
"source": [
48-
"Now you can write some Q# code. This will be the Q# code for which we will generate a circuit diagram."
48+
"Now we can write some Q# code. This will be the Q# code for which we will generate a circuit diagram. We will set up two test code snippets - one that consists of a single operation, and another that has an operation calling another operation."
4949
]
5050
},
5151
{
@@ -55,6 +55,23 @@
5555
"outputs": [],
5656
"source": [
5757
"code_1 = \"\"\"\n",
58+
"operation Main() : Result[] {\n",
59+
" use qubits = Qubit[8];\n",
60+
"\n",
61+
" // apply Hadamard gate to the first qubit\n",
62+
" H(qubits[0]);\n",
63+
"\n",
64+
" // apply CNOT gates to create entanglement\n",
65+
" for qubit in qubits[1..Length(qubits) - 1] {\n",
66+
" CNOT(qubits[0], qubit);\n",
67+
" }\n",
68+
"\n",
69+
" // return measurement results\n",
70+
" MResetEachZ(qubits)\n",
71+
"}\n",
72+
"\"\"\"\n",
73+
"\n",
74+
"code_2 = \"\"\"\n",
5875
"namespace MyQuantumApp {\n",
5976
" @EntryPoint()\n",
6077
" operation Run() : (Result, Result) {\n",
@@ -71,31 +88,14 @@
7188
" CNOT(q1, q2);\n",
7289
" }\n",
7390
"}\n",
74-
"\"\"\"\n",
75-
"\n",
76-
"code_2 = \"\"\"\n",
77-
"operation Main() : Result[] {\n",
78-
" use qubits = Qubit[8];\n",
79-
"\n",
80-
" // apply Hadamard gate to the first qubit\n",
81-
" H(qubits[0]);\n",
82-
"\n",
83-
" // apply CNOT gates to create entanglement\n",
84-
" for qubit in qubits[1..Length(qubits) - 1] {\n",
85-
" CNOT(qubits[0], qubit);\n",
86-
" }\n",
87-
"\n",
88-
" // return measurement results\n",
89-
" MResetEachZ(qubits)\n",
90-
"}\n",
9191
"\"\"\""
9292
]
9393
},
9494
{
9595
"cell_type": "markdown",
9696
"metadata": {},
9797
"source": [
98-
"Once the Q# code is written, you can use the `quantikz` function to generate and display the circuit diagram directly in the Jupyter notebook."
98+
"Once the Q# code is written, we can use the `quantikz` function to generate and display the circuit diagram directly in the Jupyter notebook."
9999
]
100100
},
101101
{
@@ -104,8 +104,8 @@
104104
"metadata": {},
105105
"outputs": [],
106106
"source": [
107-
"quantikz_diagram_1 = quantikz(code_1)\n",
108-
"quantikz_diagram_2 = quantikz(code_2)\n",
107+
"quantikz_diagram_1 = quantikz(code_1, options=QuantikzGenerationOptions(group_by_scope=False))\n",
108+
"quantikz_diagram_2 = quantikz(code_2, options=QuantikzGenerationOptions(group_by_scope=False))\n",
109109
"\n",
110110
"# for debugging, display the generated LaTeX code\n",
111111
"print(quantikz_diagram_1)\n",
@@ -116,7 +116,7 @@
116116
"cell_type": "markdown",
117117
"metadata": {},
118118
"source": [
119-
"Use the TikZ extension to render a PDF from the LaTeX code generated by Q# Bridge."
119+
"We can now use the TikZ extension to render a PDF from the LaTeX code generated by Q# Bridge to verify the output."
120120
]
121121
},
122122
{
@@ -143,6 +143,13 @@
143143
"shell.run_cell_magic('tikz', '-l quantikz', quantikz_diagram_2)"
144144
]
145145
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"Notice how the two-operation sample was actually decomposed into its constituent individual gates in the generated diagram - this is because we set `group_by_scope` to `False` in the `QuantikzGenerationOptions` when generating the diagrams."
151+
]
152+
},
146153
{
147154
"cell_type": "markdown",
148155
"metadata": {},
@@ -158,7 +165,7 @@
158165
"source": [
159166
"qsharp.init() # this ensures clean state\n",
160167
"qsharp.eval(code_1)\n",
161-
"Circuit(qsharp.circuit(\"MyQuantumApp.Run()\")) "
168+
"Circuit(qsharp.circuit(\"Main()\")) "
162169
]
163170
},
164171
{
@@ -170,7 +177,7 @@
170177
"\n",
171178
"qsharp.init() # this ensures clean state\n",
172179
"qsharp.eval(code_2)\n",
173-
"Circuit(qsharp.circuit(\"Main()\")) "
180+
"Circuit(qsharp.circuit(\"MyQuantumApp.Run()\")) "
174181
]
175182
},
176183
{
@@ -179,6 +186,39 @@
179186
"source": [
180187
"We should see that the diagrams generated using `quantikz` and the built-in `Circuit` widget are identical. This confirms that our LaTeX generation is working correctly!"
181188
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"Q# compiler (and, by extension, Q# Bridge) can also group gates on the circuit into groups scoped by an operation. In our case that would mean a large single block representing `PrepareBellState`.\n",
195+
"\n",
196+
"Let's set `group_by_scope` of the `QuantikzGenerationOptions` to `True` and pass that into the `quantikz` function - and see how the generated diagram changes."
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"quantikz_diagram_3 = quantikz(code_2, options=QuantikzGenerationOptions(group_by_scope=True))\n",
206+
"\n",
207+
"# for debugging, display the generated LaTeX code\n",
208+
"print(quantikz_diagram_3)\n",
209+
"\n",
210+
"shell = get_ipython()\n",
211+
"assert shell is not None\n",
212+
"\n",
213+
"shell.run_cell_magic('tikz', '-l quantikz', quantikz_diagram_3)"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"metadata": {},
219+
"source": [
220+
"Congratualations! You have successfully generated quantum circuit diagrams from Q# code using Q# Bridge and LaTeX in a Jupyter notebook."
221+
]
182222
}
183223
],
184224
"metadata": {

platforms/csharp/QSharp.Community.QSharpBridge/QSharp.Community.QSharpBridge.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<Version>0.2.0</Version>
5+
<Version>0.2.1</Version>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

platforms/python/qsharp-bridge/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
CARGO_MANIFEST_PATH = os.path.abspath(os.path.join(HERE, "../../../Cargo.toml"))
1515
CARGO_TARGET_DIR = os.path.abspath(os.path.join(HERE, "../../../target/release"))
1616
BINDINGS_SRC = os.path.abspath(os.path.join(HERE, "../../../bindings/qsharp_bridge.py"))
17-
VERSION = os.environ.get("PACKAGE_VERSION", "0.2.0")
17+
VERSION = os.environ.get("PACKAGE_VERSION", "0.2.1")
1818

1919
def get_lib_filename():
2020
"""

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::sim::run_qs;
1717
use crate::sim::run_qs_with_options;
1818
use crate::quantikz::quantikz;
1919
use crate::quantikz::quantikz_operation;
20+
use crate::quantikz::QuantikzGenerationOptions;
2021

2122
pub mod noise;
2223
pub mod qasm;

src/qsharp-bridge.udl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace qsharp_bridge {
1616
string qasm2_expression([ByRef]string expression, QasmGenerationOptions generation_options);
1717

1818
[Throws=QsError]
19-
string quantikz([ByRef]string source);
19+
string quantikz([ByRef]string source, QuantikzGenerationOptions options);
2020

2121
[Throws=QsError]
22-
string quantikz_operation([ByRef]string operation, [ByRef]string source);
22+
string quantikz_operation([ByRef]string operation, [ByRef]string source, QuantikzGenerationOptions options);
2323

2424
[Throws=QsError]
2525
string estimate([ByRef]string source, string? job_params);
@@ -33,6 +33,10 @@ dictionary QasmGenerationOptions {
3333
QasmResetBehavior reset_behavior;
3434
};
3535

36+
dictionary QuantikzGenerationOptions {
37+
boolean group_by_scope;
38+
};
39+
3640
enum QasmResetBehavior {
3741
"Supported",
3842
"Ignored",

0 commit comments

Comments
 (0)