Skip to content

Commit f61a8ce

Browse files
update cfd_qls example with removing explicity pyqsp
1 parent 57c036f commit f61a8ce

11 files changed

+329
-386
lines changed

applications/cfd/linear_qls_for_hybrid_solvers/cheb_lcu_solver_banded_be.qmod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@disable_perm_check
22
@disable_const_checks(packed_vars)
3-
qperm reflect_about_zero_expanded___0(const packed_vars: qbit[5])
4-
{
3+
qperm reflect_about_zero_expanded___0(const packed_vars: qbit[5]) {
54
msbs: qnum<4, False, 0>;
65
lsb: qbit;
76
packed_vars -> {msbs, lsb};

applications/cfd/linear_qls_for_hybrid_solvers/cheb_lcu_solver_banded_be.synthesis_options.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
{
22
"constraints": {
3+
"max_gate_count": {},
34
"optimization_parameter": "no_opt"
45
},
56
"preferences": {
67
"custom_hardware_settings": {
78
"basis_gates": [
8-
"cz",
9-
"ry",
10-
"tdg",
11-
"sdg",
12-
"y",
13-
"rz",
9+
"rx",
10+
"sxdg",
11+
"id",
1412
"h",
15-
"x",
1613
"r",
1714
"cy",
18-
"z",
19-
"cx",
20-
"sx",
15+
"rz",
16+
"x",
17+
"tdg",
18+
"ry",
19+
"p",
20+
"cz",
21+
"t",
22+
"u",
2123
"u1",
2224
"s",
23-
"sxdg",
24-
"id",
25-
"t",
26-
"p",
25+
"cx",
2726
"u2",
28-
"u",
29-
"rx"
27+
"sdg",
28+
"y",
29+
"z",
30+
"sx"
3031
],
3132
"is_symmetric_connectivity": true
3233
},
@@ -35,7 +36,7 @@
3536
"optimization_level": 1,
3637
"output_format": ["qasm"],
3738
"pretty_qasm": true,
38-
"random_seed": 967204371,
39+
"random_seed": 3197158459,
3940
"synthesize_all_separately": false,
4041
"timeout_seconds": 300,
4142
"transpilation_option": "auto optimize"

applications/cfd/linear_qls_for_hybrid_solvers/cheb_lcu_solver_pauli_be.qmod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@disable_perm_check
22
@disable_const_checks(packed_vars)
3-
qperm reflect_about_zero_expanded___0(const packed_vars: qbit[5])
4-
{
3+
qperm reflect_about_zero_expanded___0(const packed_vars: qbit[5]) {
54
msbs: qnum<4, False, 0>;
65
lsb: qbit;
76
packed_vars -> {msbs, lsb};

applications/cfd/linear_qls_for_hybrid_solvers/cheb_lcu_solver_pauli_be.synthesis_options.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
{
22
"constraints": {
3+
"max_gate_count": {},
34
"optimization_parameter": "no_opt"
45
},
56
"preferences": {
67
"custom_hardware_settings": {
78
"basis_gates": [
8-
"cz",
9-
"ry",
10-
"tdg",
11-
"sdg",
12-
"y",
13-
"rz",
9+
"rx",
10+
"sxdg",
11+
"id",
1412
"h",
15-
"x",
1613
"r",
1714
"cy",
18-
"z",
19-
"cx",
20-
"sx",
15+
"rz",
16+
"x",
17+
"tdg",
18+
"ry",
19+
"p",
20+
"cz",
21+
"t",
22+
"u",
2123
"u1",
2224
"s",
23-
"sxdg",
24-
"id",
25-
"t",
26-
"p",
25+
"cx",
2726
"u2",
28-
"u",
29-
"rx"
27+
"sdg",
28+
"y",
29+
"z",
30+
"sx"
3031
],
3132
"is_symmetric_connectivity": true
3233
},
@@ -35,7 +36,7 @@
3536
"optimization_level": 1,
3637
"output_format": ["qasm"],
3738
"pretty_qasm": true,
38-
"random_seed": 3880106879,
39+
"random_seed": 3654003723,
3940
"synthesize_all_separately": false,
4041
"timeout_seconds": 300,
4142
"transpilation_option": "auto optimize"

applications/cfd/linear_qls_for_hybrid_solvers/cheb_utils.py

Lines changed: 10 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,24 @@
11
import cvxpy as cp
22
import matplotlib.pyplot as plt
3-
import pyqsp
4-
from pyqsp.angle_sequence import QuantumSignalProcessingPhases
5-
from numpy.polynomial.chebyshev import Chebyshev
3+
from classiq.applications.qsp import qsp_approximate
64
import numpy as np
75
from scipy.special import eval_chebyt
8-
9-
10-
def get_qsvt_phases(poly, plot=False):
11-
np.random.seed(1) # set seed as pyqsp does not allow it, and not always converges
12-
ang_seq = QuantumSignalProcessingPhases(
13-
poly,
14-
# chebyshev_basis=True,
15-
signal_operator="Wx",
16-
# method="sym_qsp",
17-
measurement="x",
18-
tolerance=0.001, # relaxing the tolerance to get convergence
19-
)
20-
if plot:
21-
pyqsp.response.PlotQSPResponse(
22-
ang_seq, target=poly, signal_operator="Wx", measurement="x"
23-
)
24-
# change W(x) to R(x), as the phases are in the W(x) conventions
25-
phases = np.array(ang_seq)
26-
phases[1:-1] = phases[1:-1] - np.pi / 2
27-
phases[0] = phases[0] - np.pi / 4
28-
phases[-1] = phases[-1] + (2 * (len(phases) - 1) - 1) * np.pi / 4
29-
30-
# verify conventions. minus is due to exp(-i*phi*z) in qsvt in comparison to qsp
31-
phases = -2 * phases
32-
33-
return phases.tolist()
6+
from numpy.polynomial.chebyshev import Chebyshev
347

358

369
def optimize_inversion_polynomial(w_min, w_max, degree, scale):
37-
M = max(1000, 10 * degree)
38-
# Discretize [-1, 1] using M grid points (interpolants)
39-
xj_full = np.cos(np.pi * np.arange(M) / (M - 1)) # Chebyshev nodes on [-1, 1]
40-
41-
# Select grid points for the objective in [w_min, w_max]
42-
xj_obj = xj_full[(xj_full >= w_min) & (xj_full <= w_max)]
43-
44-
# Define the Chebyshev polynomials of odd degrees
45-
k_max = (degree - 1) // 2
46-
T_matrix_full = np.array(
47-
[
48-
[np.cos((2 * k + 1) * np.arccos(x)) for k in range(k_max + 1)]
49-
for x in xj_full
50-
]
51-
)
52-
T_matrix_obj = np.array(
53-
[[np.cos((2 * k + 1) * np.arccos(x)) for k in range(k_max + 1)] for x in xj_obj]
54-
)
55-
56-
# Define optimization variables
57-
c = cp.Variable(k_max + 1) # Coefficients for Chebyshev polynomials
58-
F_values_full = T_matrix_full @ c # Values for constraints
59-
F_values_obj = T_matrix_obj @ c # Values for the objective function
60-
61-
# Relaxed constraint
62-
# scale = 0.95
63-
# up_boundary = 0.95
64-
up_boundary = scale
6510

6611
def target_function(x):
6712
return scale * (w_min) / x
6813

69-
# Define the optimization problem
70-
objective = cp.Minimize(cp.max(cp.abs(F_values_obj - target_function(xj_obj))))
71-
constraints = [cp.abs(F_values_full) <= up_boundary]
72-
prob = cp.Problem(objective, constraints)
73-
74-
# Solve the optimization problem
75-
prob.solve()
76-
print(f"Max relative error value: {prob.value / scale}")
77-
78-
# Return coefficients, optimal value, and grid points
79-
pcoefs = np.zeros(len(c.value) * 2)
80-
pcoefs[1::2] = c.value
14+
pcoefs, opt_res = qsp_approximate(
15+
target_function,
16+
degree=degree,
17+
parity=1,
18+
interval=[w_min, w_max],
19+
plot=False,
20+
bound=0.95,
21+
)
8122

8223
return pcoefs
8324

applications/cfd/linear_qls_for_hybrid_solvers/qls_chebyshev_lcu.ipynb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": null,
18+
"execution_count": 1,
1919
"id": "1",
2020
"metadata": {},
2121
"outputs": [],
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": 1,
29+
"execution_count": 2,
3030
"id": "2",
3131
"metadata": {},
3232
"outputs": [],
@@ -48,7 +48,7 @@
4848
},
4949
{
5050
"cell_type": "code",
51-
"execution_count": 2,
51+
"execution_count": 3,
5252
"id": "3",
5353
"metadata": {},
5454
"outputs": [],
@@ -118,7 +118,7 @@
118118
},
119119
{
120120
"cell_type": "code",
121-
"execution_count": 3,
121+
"execution_count": 4,
122122
"id": "5",
123123
"metadata": {},
124124
"outputs": [],
@@ -224,7 +224,7 @@
224224
},
225225
{
226226
"cell_type": "code",
227-
"execution_count": 4,
227+
"execution_count": 5,
228228
"id": "7",
229229
"metadata": {},
230230
"outputs": [],
@@ -246,7 +246,7 @@
246246
},
247247
{
248248
"cell_type": "code",
249-
"execution_count": 5,
249+
"execution_count": 6,
250250
"id": "8",
251251
"metadata": {},
252252
"outputs": [],
@@ -355,7 +355,7 @@
355355
},
356356
{
357357
"cell_type": "code",
358-
"execution_count": 6,
358+
"execution_count": 7,
359359
"id": "9",
360360
"metadata": {},
361361
"outputs": [],
@@ -371,7 +371,7 @@
371371
},
372372
{
373373
"cell_type": "code",
374-
"execution_count": 7,
374+
"execution_count": 8,
375375
"id": "10",
376376
"metadata": {},
377377
"outputs": [
@@ -396,7 +396,7 @@
396396
},
397397
{
398398
"cell_type": "code",
399-
"execution_count": 8,
399+
"execution_count": 9,
400400
"id": "11",
401401
"metadata": {},
402402
"outputs": [],
@@ -413,7 +413,7 @@
413413
},
414414
{
415415
"cell_type": "code",
416-
"execution_count": 9,
416+
"execution_count": 10,
417417
"id": "12",
418418
"metadata": {},
419419
"outputs": [
@@ -426,9 +426,9 @@
426426
"Performing numpy Chebyshev interpolation, with degree 1293 and trimming to degree 32\n",
427427
"Chebyshec LCU size: 4 qubits.\n",
428428
"Normalization factor for inversion: 0.2754793041517278\n",
429-
"time to syn: 121.38798403739929\n",
430-
"time to exe: 59.68091106414795\n",
431-
"Quantum program link: https://platform.classiq.io/circuit/32BR1I7187rgHY3r4V1m9sbaNTe\n"
429+
"time to syn: 119.65162301063538\n",
430+
"time to exe: 47.653488874435425\n",
431+
"Quantum program link: https://platform.classiq.io/circuit/34JnoSZxm486HLiH9TooAxarS7S\n"
432432
]
433433
}
434434
],
@@ -448,7 +448,7 @@
448448
},
449449
{
450450
"cell_type": "code",
451-
"execution_count": 10,
451+
"execution_count": 11,
452452
"id": "13",
453453
"metadata": {},
454454
"outputs": [
@@ -461,9 +461,9 @@
461461
"Performing numpy Chebyshev interpolation, with degree 899 and trimming to degree 32\n",
462462
"Chebyshec LCU size: 4 qubits.\n",
463463
"Normalization factor for inversion: 0.37096704248608536\n",
464-
"time to syn: 102.73084592819214\n",
465-
"time to exe: 20.385194063186646\n",
466-
"Quantum program link: https://platform.classiq.io/circuit/32BRHueP7xyNN7fBDiMm2NSvECD\n"
464+
"time to syn: 67.54047083854675\n",
465+
"time to exe: 24.324007987976074\n",
466+
"Quantum program link: https://platform.classiq.io/circuit/34Jo0JTU4qaoJoaJQGbvEbyqlD6\n"
467467
]
468468
}
469469
],
@@ -483,17 +483,17 @@
483483
},
484484
{
485485
"cell_type": "code",
486-
"execution_count": 11,
486+
"execution_count": 12,
487487
"id": "14",
488488
"metadata": {},
489489
"outputs": [
490490
{
491491
"data": {
492492
"text/plain": [
493-
"[<matplotlib.lines.Line2D at 0x30590e390>]"
493+
"[<matplotlib.lines.Line2D at 0x132a8ff10>]"
494494
]
495495
},
496-
"execution_count": 11,
496+
"execution_count": 12,
497497
"metadata": {},
498498
"output_type": "execute_result"
499499
},
@@ -531,7 +531,7 @@
531531
},
532532
{
533533
"cell_type": "code",
534-
"execution_count": 12,
534+
"execution_count": 13,
535535
"id": "15",
536536
"metadata": {},
537537
"outputs": [],

0 commit comments

Comments
 (0)