Skip to content

Commit e77654f

Browse files
committed
update the quickstart.rst refactor by manual
1 parent f0cd358 commit e77654f

File tree

7 files changed

+281
-189
lines changed

7 files changed

+281
-189
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,6 @@ cython_debug/
194194
.cursorignore
195195
.cursorindexingignore
196196
uv.lock
197+
198+
.DS_Store
199+

docs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# You can set these variables from the command line.
55
SPHINXOPTS =
66
SPHINXBUILD = sphinx-build
7-
SPHINXPROJ = tensorcircuit
7+
SPHINXPROJ = tyxonq
88
SOURCEDIR = source
99
BUILDDIR = build
1010

docs/requirements-rtd.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Sphinx>=8.1.3
2+
sphinx-copybutton>=0.5.2
3+
nbsphinx>=0.9.0
4+
myst_parser>=2.0.0
5+
sphinx_design>=0.5.0
6+
furo>=2025.8.18.1
7+
pypandoc>=1.11.0
8+
ipykernel>=6.26.0
9+
10+
#for macox install pandoc
11+
#brew install pandoc
12+
#for Linux install pandoc
13+
#sudo apt-get install pandoc
14+
15+
#python -m ipykernel install --user --name python

docs/source/quickstart.rst

Lines changed: 133 additions & 184 deletions
Large diffs are not rendered by default.

docs/source/tutorials/qcloud_sdk.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,9 @@
12931293
],
12941294
"metadata": {
12951295
"kernelspec": {
1296-
"display_name": "Python 3 (ipykernel)",
1296+
"display_name": "rtd",
12971297
"language": "python",
1298-
"name": "python3"
1298+
"name": "python"
12991299
},
13001300
"language_info": {
13011301
"codemirror_mode": {
@@ -1307,7 +1307,7 @@
13071307
"name": "python",
13081308
"nbconvert_exporter": "python",
13091309
"pygments_lexer": "ipython3",
1310-
"version": "3.8.0"
1310+
"version": "3.10.18"
13111311
}
13121312
},
13131313
"nbformat": 4,

examples/hybrid_gpu_pipeline.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
Quantum part in PyTorch, neural part in PyTorch, both on GPU
3+
Hybrid quantum-classical pipeline demonstration
4+
"""
5+
6+
import os
7+
import time
8+
import numpy as np
9+
import torch
10+
import torchvision
11+
import tyxonq as tq
12+
13+
# Set PyTorch device
14+
if torch.cuda.is_available():
15+
device = torch.device("cuda")
16+
else:
17+
device = torch.device("cpu")
18+
print(device)
19+
20+
# Set quantum computing backend to PyTorch
21+
K = tq.set_backend("pytorch")
22+
23+
# Prepare dataset using torchvision
24+
train_dataset = torchvision.datasets.MNIST(
25+
root='./data',
26+
train=True,
27+
download=True,
28+
transform=torchvision.transforms.ToTensor()
29+
)
30+
31+
test_dataset = torchvision.datasets.MNIST(
32+
root='./data',
33+
train=False,
34+
download=True,
35+
transform=torchvision.transforms.ToTensor()
36+
)
37+
38+
# Convert to NumPy arrays to maintain original processing logic
39+
x_train = train_dataset.data.numpy()
40+
y_train = train_dataset.targets.numpy()
41+
x_test = test_dataset.data.numpy()
42+
y_test = test_dataset.targets.numpy()
43+
44+
x_train = x_train[..., np.newaxis] / 255.0
45+
46+
def filter_pair(x, y, a, b):
47+
"""Filter dataset to only include two specified classes"""
48+
keep = (y == a) | (y == b)
49+
x, y = x[keep], y[keep]
50+
y = y == a
51+
return x, y
52+
53+
x_train, y_train = filter_pair(x_train, y_train, 1, 5)
54+
55+
# Use torch.nn.functional.interpolate
56+
import torch.nn.functional as F
57+
x_train_small = F.interpolate(
58+
torch.tensor(x_train).permute(0, 3, 1, 2),
59+
size=(3, 3),
60+
mode='bilinear',
61+
align_corners=False
62+
).permute(0, 2, 3, 1).numpy()
63+
64+
x_train_bin = np.array(x_train_small > 0.5, dtype=np.float32)
65+
x_train_bin = np.squeeze(x_train_bin).reshape([-1, 9])
66+
y_train_torch = torch.tensor(y_train, dtype=torch.float32)
67+
x_train_torch = torch.tensor(x_train_bin)
68+
x_train_torch = x_train_torch.to(device=device)
69+
y_train_torch = y_train_torch.to(device=device)
70+
71+
n = 9
72+
nlayers = 3
73+
74+
# Define the quantum function (using PyTorch backend)
75+
def qpreds(x, weights):
76+
"""Quantum circuit for predictions"""
77+
c = tq.Circuit(n)
78+
for i in range(n):
79+
c.rx(i, theta=x[i])
80+
for j in range(nlayers):
81+
for i in range(n - 1):
82+
c.cnot(i, i + 1)
83+
for i in range(n):
84+
c.rx(i, theta=weights[2 * j, i])
85+
c.ry(i, theta=weights[2 * j + 1, i])
86+
87+
return K.stack([K.real(c.expectation_ps(z=[i])) for i in range(n)])
88+
89+
# Create quantum neural network layer
90+
quantumnet = tq.TorchLayer(
91+
qpreds,
92+
weights_shape=[2 * nlayers, n],
93+
use_vmap=True,
94+
use_interface=True,
95+
use_jit=True,
96+
enable_dlpack=True, # Enable DLPack for efficient data transfer
97+
)
98+
99+
model = torch.nn.Sequential(quantumnet, torch.nn.Linear(9, 1), torch.nn.Sigmoid())
100+
model = model.to(device=device)
101+
102+
criterion = torch.nn.BCELoss()
103+
opt = torch.optim.Adam(model.parameters(), lr=1e-2)
104+
nepochs = 300
105+
nbatch = 32
106+
times = []
107+
for epoch in range(nepochs):
108+
index = np.random.randint(low=0, high=100, size=nbatch)
109+
inputs, labels = x_train_torch[index], y_train_torch[index]
110+
opt.zero_grad()
111+
112+
with torch.set_grad_enabled(True):
113+
time0 = time.time()
114+
yps = model(inputs)
115+
loss = criterion(
116+
torch.reshape(yps, [nbatch, 1]), torch.reshape(labels, [nbatch, 1])
117+
)
118+
loss.backward()
119+
if epoch % 100 == 0:
120+
print(loss)
121+
opt.step()
122+
time1 = time.time()
123+
times.append(time1 - time0)
124+
print("Training time per step: ", np.mean(times[1:]))

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "tyxonq"
3-
version = "0.1.1"
3+
version = "0.4.0"
44
description = "Quantum computing framework with multi-backend support"
55
readme = "README.md"
66
requires-python = ">=3.10"
@@ -36,6 +36,7 @@ dependencies = [
3636
"tensorflow>=2.19.0",
3737
"tensornetwork>=0.4.6",
3838
"torch>=2.7.1",
39+
"torchvision>=0.22",
3940
]
4041

4142
[project.optional-dependencies]

0 commit comments

Comments
 (0)