Skip to content

Commit fbc1363

Browse files
doctorperceptronmariaschuldAlan-eMartinAlan Martindaniela-angulo
authored
Tutorial on resourcefulness (CI fix) (#1577)
This is a direct copy of PR #1462. That PR was open before the switch to the V2 pipeline which has caused CI to stall. --------- Co-authored-by: mariaschuld <mariaschuld@gmail.com> Co-authored-by: Alan Martin <53958929+Alan-eMartin@users.noreply.github.com> Co-authored-by: Alan Martin <alan.martin@xanadu.ai> Co-authored-by: Daniela Angulo <42325731+daniela-angulo@users.noreply.github.com>
1 parent 3b74ce5 commit fbc1363

File tree

7 files changed

+880
-0
lines changed

7 files changed

+880
-0
lines changed
18.2 KB
Loading
15.9 KB
Loading
3.36 KB
Loading
228 KB
Loading

demonstrations_v2/tutorial_resourcefulness/demo.py

Lines changed: 759 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"title": "Resourcefulness of quantum states with Fourier analysis",
3+
"authors": [
4+
{
5+
"username": "impolster"
6+
},
7+
{
8+
"username": "mariaschuld"
9+
}
10+
],
11+
"executable_stable": true,
12+
"executable_latest": true,
13+
"dateOfPublication": "2025-10-30T09:00:00+00:00",
14+
"dateOfLastModification": "2025-10-30T09:00:00+00:00",
15+
"categories": [
16+
"Quantum Computing"
17+
],
18+
"tags": [],
19+
"previewImages": [
20+
{
21+
"type": "thumbnail",
22+
"uri": "/_static/demo_thumbnails/regular_demo_thumbnails/pennylane-demo-resourcefulness-thumbnail.png"
23+
},
24+
{
25+
"type": "large_thumbnail",
26+
"uri": "/_static/demo_thumbnails/large_demo_thumbnails/pennylane-demo-resourcefulness-large-thumbnail.png"
27+
}
28+
],
29+
"seoDescription": "Find out how entangled a state is by looking at its generalised Fourier spectrum.",
30+
"doi": "",
31+
"references": [
32+
{
33+
"id": "Bermejo_Braccia",
34+
"type": "preprint",
35+
"title": "Characterizing quantum resourcefulness via group-Fourier decompositions",
36+
"authors": "Pablo Bermejo, Paolo Braccia, Antonio Anna Mele, N. L. Diaz, Andrew E. Deneris, Martín Larocca, Marco Cerezo",
37+
"year": "2025",
38+
"url": "https://arxiv.org/pdf/2506.19696"
39+
}
40+
],
41+
"basedOnPapers": ["10.48550/arXiv.2506.19696"],
42+
"referencedByPapers": [],
43+
"relatedContent": [
44+
{
45+
"type": "demonstration",
46+
"id": "tutorial_resourcefulness",
47+
"weight": 1.0
48+
},
49+
{
50+
"type": "demonstration",
51+
"id": "tutorial_resourcefulness_with_fourier_transform",
52+
"weight": 1.0
53+
}
54+
]
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Utility function that sorts a block-diagonal matrix for visualisation purposes.
3+
"""
4+
5+
import numpy as np
6+
from collections import OrderedDict
7+
8+
def group_rows_cols_by_sparsity(B, tol=0):
9+
"""
10+
Given matrix B, this function groups identical rows and columns, orders these groups
11+
by sparsity (most zeros first) and returns the row & column permutation
12+
matrices P_row, P_col such that B2 = P_row @ B @ P_col is block-diagonal.
13+
"""
14+
# compute boolean mask where |B| >= tol
15+
mask = np.abs(B) >= 1e-8
16+
# convert boolean mask to integer (False→0, True→1)
17+
C = mask.astype(int)
18+
19+
# order by sparsity
20+
n, m = C.shape
21+
22+
# helper to get a key tuple and zero count for a vector
23+
def key_and_zeros(vec):
24+
if tol > 0:
25+
bin_vec = (np.abs(vec) < tol).astype(int)
26+
key = tuple(bin_vec)
27+
zero_count = int(np.sum(bin_vec))
28+
else:
29+
key = tuple(vec.tolist())
30+
zero_count = int(np.sum(np.array(vec) == 0))
31+
return key, zero_count
32+
33+
# group rows by key
34+
row_groups = OrderedDict()
35+
row_zero_counts = {}
36+
for i in range(n):
37+
key, zc = key_and_zeros(C[i, :])
38+
row_groups.setdefault(key, []).append(i)
39+
row_zero_counts[key] = zc
40+
41+
# sort row groups by zero_count descending
42+
sorted_row_keys = sorted(row_groups.keys(),
43+
key=lambda k: row_zero_counts[k],
44+
reverse=True)
45+
# flatten row permutation
46+
row_perm = [i for key in sorted_row_keys for i in row_groups[key]]
47+
48+
# group columns by key
49+
col_groups = OrderedDict()
50+
col_zero_counts = {}
51+
for j in range(m):
52+
key, zc = key_and_zeros(C[:, j])
53+
col_groups.setdefault(key, []).append(j)
54+
col_zero_counts[key] = zc
55+
56+
# sort column groups by zero_count descending
57+
sorted_col_keys = sorted(col_groups.keys(),
58+
key=lambda k: col_zero_counts[k],
59+
reverse=True)
60+
col_perm = [j for key in sorted_col_keys for j in col_groups[key]]
61+
62+
# build permutation matrices
63+
P_row = np.eye(n)[row_perm, :]
64+
P_col = np.eye(m)[:, col_perm]
65+
66+
return P_row, P_col

0 commit comments

Comments
 (0)