Spaces:
Paused
Paused
Added new initial dist (multi_dirac_delta); fixed mistake with velocity field implementation
Browse files- =4.2.0 +21 -0
- qlbm/qlbm_sample_app.py +84 -51
=4.2.0
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Collecting nbformat
|
| 2 |
+
Using cached nbformat-5.10.4-py3-none-any.whl (78 kB)
|
| 3 |
+
Collecting fastjsonschema>=2.15
|
| 4 |
+
Using cached fastjsonschema-2.21.2-py3-none-any.whl (24 kB)
|
| 5 |
+
Requirement already satisfied: traitlets>=5.1 in /home/jezerjojo14/hf_demo_new/.venv/lib/python3.10/site-packages (from nbformat) (5.14.3)
|
| 6 |
+
Collecting jupyter-core!=5.0.*,>=4.12
|
| 7 |
+
Using cached jupyter_core-5.9.1-py3-none-any.whl (29 kB)
|
| 8 |
+
Collecting jsonschema>=2.6
|
| 9 |
+
Using cached jsonschema-4.25.1-py3-none-any.whl (90 kB)
|
| 10 |
+
Requirement already satisfied: attrs>=22.2.0 in /home/jezerjojo14/hf_demo_new/.venv/lib/python3.10/site-packages (from jsonschema>=2.6->nbformat) (25.4.0)
|
| 11 |
+
Collecting rpds-py>=0.7.1
|
| 12 |
+
Downloading rpds_py-0.30.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (390 kB)
|
| 13 |
+
ββββββββββββββββββββββββββββββββββββββββ 390.5/390.5 KB 7.5 MB/s eta 0:00:00
|
| 14 |
+
Collecting referencing>=0.28.4
|
| 15 |
+
Using cached referencing-0.37.0-py3-none-any.whl (26 kB)
|
| 16 |
+
Collecting jsonschema-specifications>=2023.03.6
|
| 17 |
+
Using cached jsonschema_specifications-2025.9.1-py3-none-any.whl (18 kB)
|
| 18 |
+
Requirement already satisfied: platformdirs>=2.5 in /home/jezerjojo14/hf_demo_new/.venv/lib/python3.10/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (4.5.0)
|
| 19 |
+
Requirement already satisfied: typing-extensions>=4.4.0 in /home/jezerjojo14/hf_demo_new/.venv/lib/python3.10/site-packages (from referencing>=0.28.4->jsonschema>=2.6->nbformat) (4.15.0)
|
| 20 |
+
Installing collected packages: fastjsonschema, rpds-py, jupyter-core, referencing, jsonschema-specifications, jsonschema, nbformat
|
| 21 |
+
Successfully installed fastjsonschema-2.21.2 jsonschema-4.25.1 jsonschema-specifications-2025.9.1 jupyter-core-5.9.1 nbformat-5.10.4 referencing-0.37.0 rpds-py-0.30.0
|
qlbm/qlbm_sample_app.py
CHANGED
|
@@ -253,6 +253,13 @@ def stream(qc,pos_qr,dir_qr,n):
|
|
| 253 |
|
| 254 |
def get_circuit(n,ux,uy,uz,init_state_prep_circ,T_list,vel_resolution=32,measure=True):
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
dirs=[[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]]
|
| 258 |
wts = np.array([2/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8])
|
|
@@ -277,20 +284,32 @@ def get_circuit(n,ux,uy,uz,init_state_prep_circ,T_list,vel_resolution=32,measure
|
|
| 277 |
|
| 278 |
qc.compose(init_state_prep_circ,[qubit for qr in pos_qr for qubit in list(qr)], inplace=True)
|
| 279 |
|
|
|
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
for T in list(range(T_total))[::-1]:
|
| 285 |
|
| 286 |
prep(qc,pos_qr,dir_qr)
|
|
|
|
|
|
|
|
|
|
| 287 |
stream(qc,pos_qr,dir_qr,n)
|
|
|
|
|
|
|
|
|
|
| 288 |
unprep(qc,pos_qr,dir_qr)
|
| 289 |
|
| 290 |
qc.measure(dir_qr,dir_cr[T])
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
|
|
|
| 294 |
|
| 295 |
if measure:
|
| 296 |
for i in range(dim):
|
|
@@ -332,10 +351,14 @@ def get_named_init_state_circuit(
|
|
| 332 |
gauss_cy: float = None, # Center Y (0-1 normalized), defaults to 0.5
|
| 333 |
gauss_cz: float = None, # Center Z (0-1 normalized), defaults to 0.5
|
| 334 |
gauss_sigma: float = None, # Spread, defaults to 0.2 in normalized units
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
):
|
| 336 |
"""
|
| 337 |
Create initial state preparation circuit with configurable parameters.
|
| 338 |
-
|
| 339 |
Parameters
|
| 340 |
----------
|
| 341 |
n : int
|
|
@@ -348,7 +371,8 @@ def get_named_init_state_circuit(
|
|
| 348 |
Center coordinates in [0,1] for Gaussian (default=0.5)
|
| 349 |
gauss_sigma : float
|
| 350 |
Spread of Gaussian in normalized units (default=0.2)
|
| 351 |
-
|
|
|
|
| 352 |
Returns
|
| 353 |
-------
|
| 354 |
QuantumCircuit
|
|
@@ -362,6 +386,14 @@ def get_named_init_state_circuit(
|
|
| 362 |
init_state_prep_circ.x(2*n-1)
|
| 363 |
init_state_prep_circ.x(3*n-1)
|
| 364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
elif init_state_name == "sin":
|
| 366 |
# Configurable frequency sinusoidal distribution
|
| 367 |
# f(x,y,z) = 1 + sin(2Ο * kx * x) * sin(2Ο * ky * y) * sin(2Ο * kz * z)
|
|
@@ -486,12 +518,12 @@ def run_sampling_hw_ibm(
|
|
| 486 |
else:
|
| 487 |
print(msg)
|
| 488 |
|
| 489 |
-
if type(ux)==str:
|
| 490 |
-
|
| 491 |
|
| 492 |
-
# Convert string init_state_prep_circ to circuit if needed (matches original logic)
|
| 493 |
-
if type(init_state_prep_circ)==str:
|
| 494 |
-
|
| 495 |
|
| 496 |
qc_list=get_circuit(n,ux,uy,uz,init_state_prep_circ,T_list,vel_resolution)
|
| 497 |
|
|
@@ -580,13 +612,13 @@ def run_sampling_sim(
|
|
| 580 |
Plotly figure with slider animation through all timesteps (includes T=0 snapshot when available)
|
| 581 |
"""
|
| 582 |
|
| 583 |
-
if type(ux)==str:
|
| 584 |
-
|
| 585 |
|
| 586 |
-
# Convert string init_state_prep_circ to circuit if needed (matches original logic)
|
| 587 |
init_state_label = init_state_prep_circ if isinstance(init_state_prep_circ, str) else "custom"
|
| 588 |
-
if isinstance(init_state_prep_circ, str):
|
| 589 |
-
init_state_prep_circ=get_named_init_state_circuit(n,init_state_prep_circ)
|
| 590 |
|
| 591 |
initial_snapshot = None
|
| 592 |
try:
|
|
@@ -892,51 +924,52 @@ def show_initial_distribution(
|
|
| 892 |
|
| 893 |
if __name__=="__main__":
|
| 894 |
|
| 895 |
-
n=
|
| 896 |
|
| 897 |
# # Step 1: Create the initial state circuit ONCE with all parameters
|
| 898 |
-
init_state_prep_circ = get_named_init_state_circuit(
|
| 899 |
-
n=n,
|
| 900 |
-
init_state_name="sin", # or "gaussian", "dirac_delta"
|
| 901 |
-
sine_k_x=1.0,
|
| 902 |
-
sine_k_y=1.0,
|
| 903 |
-
sine_k_z=1.0
|
| 904 |
-
# gauss_cx=0.5, # Uncomment for Gaussian
|
| 905 |
-
# gauss_cy=0.5,
|
| 906 |
-
# gauss_cz=0.5,
|
| 907 |
-
# gauss_sigma=0.2,
|
| 908 |
-
)
|
| 909 |
-
|
| 910 |
-
# # Alternative: Run on local simulator
|
| 911 |
-
# output, fig = run_sampling_sim(
|
| 912 |
# n=n,
|
| 913 |
-
#
|
| 914 |
-
#
|
| 915 |
-
#
|
| 916 |
-
#
|
| 917 |
-
#
|
| 918 |
-
#
|
|
|
|
|
|
|
| 919 |
# )
|
| 920 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 921 |
|
| 922 |
# Step 2: (Optional) Preview the initial distribution
|
| 923 |
# show_initial_distribution(n=n, init_state_name="sin", sine_k_x=1, sine_k_y=1, sine_k_z=1)
|
| 924 |
|
| 925 |
# Step 3: Run simulation - pass the pre-built circuit
|
| 926 |
-
job, get_job_result = run_sampling_hw_ibm(
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
)
|
| 936 |
|
| 937 |
-
output = get_job_result(job)
|
| 938 |
-
for xx, yy, zz, dens in output:
|
| 939 |
-
|
| 940 |
|
| 941 |
|
| 942 |
|
|
|
|
| 253 |
|
| 254 |
def get_circuit(n,ux,uy,uz,init_state_prep_circ,T_list,vel_resolution=32,measure=True):
|
| 255 |
|
| 256 |
+
ux_str,uy_str,uz_str=None,None,None
|
| 257 |
+
if type(ux)==str:
|
| 258 |
+
ux_str,uy_str,uz_str=ux,uy,uz
|
| 259 |
+
ux,uy,uz=str_to_lambda(ux_str,uy_str,uz_str)
|
| 260 |
+
|
| 261 |
+
if type(init_state_prep_circ)==str:
|
| 262 |
+
init_state_prep_circ=get_named_init_state_circuit(n,init_state_prep_circ)
|
| 263 |
|
| 264 |
dirs=[[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]]
|
| 265 |
wts = np.array([2/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8])
|
|
|
|
| 284 |
|
| 285 |
qc.compose(init_state_prep_circ,[qubit for qr in pos_qr for qubit in list(qr)], inplace=True)
|
| 286 |
|
| 287 |
+
uniform_bool=False
|
| 288 |
|
| 289 |
+
if 'x' not in ux_str+uy_str+uz_str and 'y' not in ux_str+uy_str+uz_str and 'z' not in ux_str+uy_str+uz_str:
|
| 290 |
+
uniform_bool=True
|
| 291 |
+
|
| 292 |
+
if uniform_bool:
|
| 293 |
+
for i in range(dim):
|
| 294 |
+
qc.compose(QFT(n, inverse=False, do_swaps=False), pos_qr[i], inplace=True)
|
| 295 |
|
| 296 |
for T in list(range(T_total))[::-1]:
|
| 297 |
|
| 298 |
prep(qc,pos_qr,dir_qr)
|
| 299 |
+
if not uniform_bool:
|
| 300 |
+
for i in range(dim):
|
| 301 |
+
qc.compose(QFT(n, inverse=False, do_swaps=False), pos_qr[i], inplace=True)
|
| 302 |
stream(qc,pos_qr,dir_qr,n)
|
| 303 |
+
if not uniform_bool:
|
| 304 |
+
for i in range(dim):
|
| 305 |
+
qc.compose(QFT(n, inverse=True, do_swaps=False), pos_qr[i], inplace=True)
|
| 306 |
unprep(qc,pos_qr,dir_qr)
|
| 307 |
|
| 308 |
qc.measure(dir_qr,dir_cr[T])
|
| 309 |
|
| 310 |
+
if uniform_bool:
|
| 311 |
+
for i in range(dim):
|
| 312 |
+
qc.compose(QFT(n, inverse=True, do_swaps=False), pos_qr[i], inplace=True)
|
| 313 |
|
| 314 |
if measure:
|
| 315 |
for i in range(dim):
|
|
|
|
| 351 |
gauss_cy: float = None, # Center Y (0-1 normalized), defaults to 0.5
|
| 352 |
gauss_cz: float = None, # Center Z (0-1 normalized), defaults to 0.5
|
| 353 |
gauss_sigma: float = None, # Spread, defaults to 0.2 in normalized units
|
| 354 |
+
# Multi-dirac-delta parameters
|
| 355 |
+
mdd_kx_log2: int = 1, # Integer greater than >=1. Number of dirac-deltas along x is 2^mdd_kx_log2
|
| 356 |
+
mdd_ky_log2: int = 1, # Integer greater than >=1. Number of dirac-deltas along y is 2^mdd_ky_log2
|
| 357 |
+
mdd_kz_log2: int = 1 # Integer greater than >=1. Number of dirac-deltas along z is 2^mdd_kz_log2
|
| 358 |
):
|
| 359 |
"""
|
| 360 |
Create initial state preparation circuit with configurable parameters.
|
| 361 |
+
|
| 362 |
Parameters
|
| 363 |
----------
|
| 364 |
n : int
|
|
|
|
| 371 |
Center coordinates in [0,1] for Gaussian (default=0.5)
|
| 372 |
gauss_sigma : float
|
| 373 |
Spread of Gaussian in normalized units (default=0.2)
|
| 374 |
+
mdd_kx_log2, mdd_ky_log2, mdd_kz_log2 : int
|
| 375 |
+
log2 of frequency multipliers for dirac-delta array distribution (default=1)
|
| 376 |
Returns
|
| 377 |
-------
|
| 378 |
QuantumCircuit
|
|
|
|
| 386 |
init_state_prep_circ.x(2*n-1)
|
| 387 |
init_state_prep_circ.x(3*n-1)
|
| 388 |
|
| 389 |
+
elif init_state_name == "multi_dirac_delta":
|
| 390 |
+
init_state_prep_circ.h(range(n-mdd_kx_log2,n))
|
| 391 |
+
init_state_prep_circ.x(n-1-mdd_kx_log2)
|
| 392 |
+
init_state_prep_circ.h(range(2*n-mdd_ky_log2,2*n))
|
| 393 |
+
init_state_prep_circ.x(2*n-1-mdd_ky_log2)
|
| 394 |
+
init_state_prep_circ.h(range(3*n-mdd_kz_log2,3*n))
|
| 395 |
+
init_state_prep_circ.x(3*n-1-mdd_kz_log2)
|
| 396 |
+
|
| 397 |
elif init_state_name == "sin":
|
| 398 |
# Configurable frequency sinusoidal distribution
|
| 399 |
# f(x,y,z) = 1 + sin(2Ο * kx * x) * sin(2Ο * ky * y) * sin(2Ο * kz * z)
|
|
|
|
| 518 |
else:
|
| 519 |
print(msg)
|
| 520 |
|
| 521 |
+
# if type(ux)==str:
|
| 522 |
+
# ux,uy,uz=str_to_lambda(ux,uy,uz)
|
| 523 |
|
| 524 |
+
# # Convert string init_state_prep_circ to circuit if needed (matches original logic)
|
| 525 |
+
# if type(init_state_prep_circ)==str:
|
| 526 |
+
# init_state_prep_circ=get_named_init_state_circuit(n,init_state_prep_circ)
|
| 527 |
|
| 528 |
qc_list=get_circuit(n,ux,uy,uz,init_state_prep_circ,T_list,vel_resolution)
|
| 529 |
|
|
|
|
| 612 |
Plotly figure with slider animation through all timesteps (includes T=0 snapshot when available)
|
| 613 |
"""
|
| 614 |
|
| 615 |
+
# if type(ux)==str:
|
| 616 |
+
# ux,uy,uz=str_to_lambda(ux,uy,uz)
|
| 617 |
|
| 618 |
+
# # Convert string init_state_prep_circ to circuit if needed (matches original logic)
|
| 619 |
init_state_label = init_state_prep_circ if isinstance(init_state_prep_circ, str) else "custom"
|
| 620 |
+
# if isinstance(init_state_prep_circ, str):
|
| 621 |
+
# init_state_prep_circ=get_named_init_state_circuit(n,init_state_prep_circ)
|
| 622 |
|
| 623 |
initial_snapshot = None
|
| 624 |
try:
|
|
|
|
| 924 |
|
| 925 |
if __name__=="__main__":
|
| 926 |
|
| 927 |
+
n=4
|
| 928 |
|
| 929 |
# # Step 1: Create the initial state circuit ONCE with all parameters
|
| 930 |
+
# init_state_prep_circ = get_named_init_state_circuit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 931 |
# n=n,
|
| 932 |
+
# init_state_name="multi_dirac_delta", # or "gaussian", "dirac_delta"
|
| 933 |
+
# sine_k_x=1.0,
|
| 934 |
+
# sine_k_y=1.0,
|
| 935 |
+
# sine_k_z=1.0
|
| 936 |
+
# # gauss_cx=0.5, # Uncomment for Gaussian
|
| 937 |
+
# # gauss_cy=0.5,
|
| 938 |
+
# # gauss_cz=0.5,
|
| 939 |
+
# # gauss_sigma=0.2,
|
| 940 |
# )
|
| 941 |
+
|
| 942 |
+
# Alternative: Run on local simulator
|
| 943 |
+
output, fig = run_sampling_sim(
|
| 944 |
+
n=n,
|
| 945 |
+
ux="sin(2*pi*y)",
|
| 946 |
+
uy="-sin(2*pi*x)",
|
| 947 |
+
uz="1",
|
| 948 |
+
init_state_prep_circ="multi_dirac_delta",
|
| 949 |
+
T_list=[1,3,5],
|
| 950 |
+
vel_resolution=16
|
| 951 |
+
)
|
| 952 |
+
print(output)
|
| 953 |
+
fig.show(renderer="browser")
|
| 954 |
|
| 955 |
# Step 2: (Optional) Preview the initial distribution
|
| 956 |
# show_initial_distribution(n=n, init_state_name="sin", sine_k_x=1, sine_k_y=1, sine_k_z=1)
|
| 957 |
|
| 958 |
# Step 3: Run simulation - pass the pre-built circuit
|
| 959 |
+
# job, get_job_result = run_sampling_hw_ibm(
|
| 960 |
+
# n=n,
|
| 961 |
+
# ux=lambda x,y,z: 1,
|
| 962 |
+
# uy=lambda x,y,z: 1,
|
| 963 |
+
# uz=lambda x,y,z: 1,
|
| 964 |
+
# init_state_prep_circ=init_state_prep_circ, # Pass the circuit directly
|
| 965 |
+
# T_list=[1],
|
| 966 |
+
# shots=2**19,
|
| 967 |
+
# vel_resolution=2,
|
| 968 |
+
# )
|
| 969 |
|
| 970 |
+
# output = get_job_result(job)
|
| 971 |
+
# for xx, yy, zz, dens in output:
|
| 972 |
+
# plot_density_isosurface(xx, yy, zz, dens)
|
| 973 |
|
| 974 |
|
| 975 |
|