Skip to content

Commit 4e40a7c

Browse files
cr fixes
1 parent fff1546 commit 4e40a7c

File tree

9 files changed

+188
-193
lines changed

9 files changed

+188
-193
lines changed

algorithms/qml/qsvm/qsvm.ipynb

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,8 @@
5252
"metadata": {},
5353
"outputs": [],
5454
"source": [
55-
"## Classiq imports\n",
56-
"\n",
5755
"import matplotlib.pyplot as plt\n",
5856
"import numpy as np\n",
59-
"\n",
60-
"## svm imports for train and validate\n",
6157
"from sklearn.svm import SVC\n",
6258
"\n",
6359
"from classiq import *"
@@ -199,12 +195,12 @@
199195
"\n",
200196
"A feature map is a way to encode classical data into quantum.\n",
201197
"Here, we chose to encode the data onto the surface of the bloch sphere.\n",
202-
"Behind the scenes, this can be translated to:\n",
198+
"This can be defined as:\n",
203199
"```\n",
204200
"R_X(x[0] / 2)\n",
205201
"R_Z(x[1])\n",
206202
"```\n",
207-
"Where `x` is the 2D input vector, and the circuit takes a single qubit per data-point. We define a quantum function that generalizes the Bloch sphere mapping to input vector of any dimension (also known as \"dense angle encoding\" in the field of Quantum Nueral Networks)."
203+
"Where `x` is the 2D input vector, and the circuit takes a single qubit per data-point. This creates a state which is: $\\cos(x[0]/4)|0\\rangle + e^{x[1]/4}\\sin(x[0]/4)|1\\rangle$ (up to a global phase). We define a quantum function that generalizes the Bloch sphere mapping to input vector of any dimension (also known as \"dense angle encoding\" in the field of Quantum Nueral Networks): each pair of entries in the vector is mapped to a Bloch sphere, in the case of odd size we apply a single RX gate on an extra qubit."
208204
]
209205
},
210206
{
@@ -218,7 +214,7 @@
218214
"\n",
219215
"\n",
220216
"@qfunc\n",
221-
"def my_bloch_feature_map(data: CArray[CReal], qba: QArray[QBit]):\n",
217+
"def bloch_feature_map(data: CArray[CReal], qba: QArray[QBit]):\n",
222218
" repeat(ceiling(data.len / 2), lambda i: RX(data[2 * i] / 2, qba[i]))\n",
223219
" repeat(floor(data.len / 2), lambda i: RZ(data[(2 * i) + 1], qba[i]))"
224220
]
@@ -256,7 +252,7 @@
256252
"metadata": {},
257253
"source": [
258254
"### Construct a model\n",
259-
"We can now construct the QSVM model using the `my_bloch_feature_map` function, and its inverse:"
255+
"We can now construct the QSVM model using the `bloch_feature_map` function, and its inverse:"
260256
]
261257
},
262258
{
@@ -277,21 +273,11 @@
277273
"):\n",
278274
"\n",
279275
" allocate(ceiling(data1.len / 2), qba)\n",
280-
" my_bloch_feature_map(data1, qba)\n",
281-
" invert(lambda: my_bloch_feature_map(data2, qba))\n",
276+
" bloch_feature_map(data1, qba)\n",
277+
" invert(lambda: bloch_feature_map(data2, qba))\n",
282278
"\n",
283279
"\n",
284-
"QSVM_BLOCH_SHPERE = create_model(main, out_file=\"qsvm\")"
285-
]
286-
},
287-
{
288-
"cell_type": "code",
289-
"execution_count": 8,
290-
"id": "d9497b62-577d-4d27-bcdb-1be89bad4546",
291-
"metadata": {},
292-
"outputs": [],
293-
"source": [
294-
"write_qmod(QSVM_BLOCH_SHPERE, \"qsvm\")"
280+
"QSVM_BLOCH_SHPERE_qmod = create_model(main, out_file=\"qsvm\")"
295281
]
296282
},
297283
{
@@ -306,20 +292,20 @@
306292
},
307293
{
308294
"cell_type": "code",
309-
"execution_count": 9,
295+
"execution_count": 8,
310296
"id": "c9cdefbe",
311297
"metadata": {},
312298
"outputs": [
313299
{
314300
"name": "stdout",
315301
"output_type": "stream",
316302
"text": [
317-
"Opening: https://platform.classiq.io/circuit/2rJdx2JxqcGgCHGbmSKSaxKLZg2?version=0.64.0\n"
303+
"Opening: https://platform.classiq.io/circuit/2rMZUsGqPxLANP3DjFBG3cYMIq7?version=0.65.1\n"
318304
]
319305
}
320306
],
321307
"source": [
322-
"qprog = synthesize(QSVM_BLOCH_SHPERE)\n",
308+
"qprog = synthesize(QSVM_BLOCH_SHPERE_qmod)\n",
323309
"show(qprog)"
324310
]
325311
},
@@ -338,35 +324,31 @@
338324
},
339325
{
340326
"cell_type": "code",
341-
"execution_count": 10,
327+
"execution_count": 9,
342328
"id": "48f3a4bd",
343329
"metadata": {},
344330
"outputs": [],
345331
"source": [
346-
"def set_execution_params(data1, data2=None, train=False):\n",
332+
"def get_execution_params(data1, data2=None):\n",
347333
" \"\"\"\n",
348334
" Generate execution parameters based on the mode (train or validate).\n",
349335
"\n",
350336
" Parameters:\n",
351337
" - data1: First dataset (used for both training and validation).\n",
352-
" - data2: Second dataset (only required for validation when train=False).\n",
353-
" - train: Boolean flag. If True, generates symmetric pairs for training.\n",
338+
" - data2: Second dataset (only required for validation).\n",
354339
"\n",
355340
" Returns:\n",
356341
" - A list of dictionaries with execution parameters.\n",
357342
" \"\"\"\n",
358-
" if train:\n",
359-
" # Training mode (symmetric pairs, data2 is ignored)\n",
343+
" if data2 is None:\n",
344+
" # Training mode (symmetric pairs of data1)\n",
360345
" return [\n",
361346
" {\"data1\": data1[k], \"data2\": data1[j]}\n",
362347
" for k in range(len(data1))\n",
363348
" for j in range(k, len(data1)) # Avoid symmetric pairs\n",
364349
" ]\n",
365350
" else:\n",
366-
" # Validation mode\n",
367-
" assert (\n",
368-
" data2 is not None\n",
369-
" ), \"For validation, data2 must be provided explicitly or set equal to data1.\"\n",
351+
" # Prediction mode\n",
370352
" return [\n",
371353
" {\"data1\": data1[k], \"data2\": data2[j]}\n",
372354
" for k in range(len(data1))\n",
@@ -376,7 +358,7 @@
376358
"\n",
377359
"def construct_kernel_matrix(matrix_size, res_batch, train=False):\n",
378360
" \"\"\"\n",
379-
" Construct a kernel matrix from `res_batch`, depending on whether it's for training or validation.\n",
361+
" Construct a kernel matrix from `res_batch`, depending on whether it's for training or predicting.\n",
380362
"\n",
381363
" Parameters:\n",
382364
" - matrix_size: Tuple of (number of rows, number of columns) for the matrix.\n",
@@ -387,7 +369,7 @@
387369
" - A kernel matrix as a NumPy array.\n",
388370
" \"\"\"\n",
389371
" rows, cols = matrix_size\n",
390-
" my_kernel_matrix = np.zeros((rows, cols))\n",
372+
" kernel_matrix = np.zeros((rows, cols))\n",
391373
"\n",
392374
" num_shots = res_batch[0].num_shots\n",
393375
" num_output_qubits = len(next(iter(res_batch[0].counts)))\n",
@@ -400,19 +382,19 @@
400382
" value = (\n",
401383
" res_batch[count].counts.get(\"0\" * num_output_qubits, 0) / num_shots\n",
402384
" )\n",
403-
" my_kernel_matrix[k, j] = value\n",
404-
" my_kernel_matrix[j, k] = value # Use symmetry\n",
385+
" kernel_matrix[k, j] = value\n",
386+
" kernel_matrix[j, k] = value # Use symmetry\n",
405387
" count += 1\n",
406388
" else:\n",
407389
" # Non-symmetric matrix (validation)\n",
408390
" for k in range(rows):\n",
409391
" for j in range(cols):\n",
410-
" my_kernel_matrix[k, j] = (\n",
392+
" kernel_matrix[k, j] = (\n",
411393
" res_batch[count].counts.get(\"0\" * num_output_qubits, 0) / num_shots\n",
412394
" )\n",
413395
" count += 1\n",
414396
"\n",
415-
" return my_kernel_matrix\n",
397+
" return kernel_matrix\n",
416398
"\n",
417399
"\n",
418400
"def train_svm(es, train_data, train_labels):\n",
@@ -422,13 +404,13 @@
422404
" Parameters:\n",
423405
" - es: ExecutionSession object to process batch execution for kernel computation.\n",
424406
" - train_data: List of data points for training.\n",
425-
" - train_labels: List of labels corresponding to the training data.\n",
407+
" - train_labels: List of binary labels corresponding to the training data.\n",
426408
"\n",
427409
" Returns:\n",
428410
" - svm_model: A trained SVM model using the precomputed kernel.\n",
429411
" \"\"\"\n",
430412
" train_size = len(train_data)\n",
431-
" train_execution_params = set_execution_params(train_data, train=True)\n",
413+
" train_execution_params = get_execution_params(train_data)\n",
432414
" res_train_batch = es.batch_sample(train_execution_params) # execute batch\n",
433415
" # generate kernel matrix for train\n",
434416
" kernel_train = construct_kernel_matrix(\n",
@@ -457,7 +439,7 @@
457439
" \"\"\"\n",
458440
" predict_size = len(data)\n",
459441
" train_size = len(train_data)\n",
460-
" predict_execution_params = set_execution_params(data, train_data, train=False)\n",
442+
" predict_execution_params = get_execution_params(data, train_data)\n",
461443
" res_predict_batch = es.batch_sample(predict_execution_params) # execute batch\n",
462444
" kernel_predict = construct_kernel_matrix(\n",
463445
" matrix_size=(predict_size, train_size), res_batch=res_predict_batch, train=False\n",
@@ -479,7 +461,7 @@
479461
},
480462
{
481463
"cell_type": "code",
482-
"execution_count": 11,
464+
"execution_count": 10,
483465
"id": "2a05189f-5875-45f2-9aef-63e2e65a621c",
484466
"metadata": {},
485467
"outputs": [],
@@ -510,7 +492,7 @@
510492
},
511493
{
512494
"cell_type": "code",
513-
"execution_count": 12,
495+
"execution_count": 11,
514496
"id": "6d4b68fc",
515497
"metadata": {},
516498
"outputs": [
@@ -536,7 +518,7 @@
536518
},
537519
{
538520
"cell_type": "code",
539-
"execution_count": 13,
521+
"execution_count": 12,
540522
"id": "a0c29c77-fa68-4d74-87ae-10a40af9af31",
541523
"metadata": {},
542524
"outputs": [
@@ -566,7 +548,7 @@
566548
},
567549
{
568550
"cell_type": "code",
569-
"execution_count": 14,
551+
"execution_count": 13,
570552
"id": "00db178e-4d49-4e55-8144-ba424b3fe833",
571553
"metadata": {},
572554
"outputs": [

algorithms/qml/qsvm/qsvm.qmod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
qfunc my_bloch_feature_map(data: real[], qba: qbit[]) {
1+
qfunc bloch_feature_map(data: real[], qba: qbit[]) {
22
repeat (i: ceiling(data.len / 2)) {
33
RX(data[2 * i] / 2, qba[i]);
44
}
@@ -9,8 +9,8 @@ qfunc my_bloch_feature_map(data: real[], qba: qbit[]) {
99

1010
qfunc main(data1: real[2], data2: real[2], output qba: qnum) {
1111
allocate(ceiling(data1.len / 2), qba);
12-
my_bloch_feature_map(data1, qba);
12+
bloch_feature_map(data1, qba);
1313
invert {
14-
my_bloch_feature_map(data2, qba);
14+
bloch_feature_map(data2, qba);
1515
}
1616
}

algorithms/qml/qsvm/qsvm.synthesis_options.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
"machine_precision": 8,
88
"custom_hardware_settings": {
99
"basis_gates": [
10-
"y",
11-
"sxdg",
12-
"z",
13-
"cz",
14-
"r",
15-
"rz",
16-
"u1",
17-
"sdg",
18-
"x",
19-
"sx",
2010
"h",
21-
"cx",
22-
"p",
23-
"tdg",
2411
"ry",
25-
"id",
26-
"cy",
12+
"sxdg",
2713
"u2",
14+
"cy",
15+
"sdg",
16+
"u",
17+
"u1",
18+
"p",
19+
"cx",
20+
"cz",
21+
"sx",
22+
"y",
2823
"rx",
2924
"t",
30-
"s",
31-
"u"
25+
"x",
26+
"tdg",
27+
"z",
28+
"r",
29+
"rz",
30+
"id",
31+
"s"
3232
],
3333
"is_symmetric_connectivity": true
3434
},
@@ -39,6 +39,6 @@
3939
"pretty_qasm": true,
4040
"transpilation_option": "auto optimize",
4141
"timeout_seconds": 300,
42-
"random_seed": 3946466952
42+
"random_seed": 3405390845
4343
}
4444
}

algorithms/qml/qsvm_pauli_feature_map/qsvm_pauli_feature_map.ipynb

Lines changed: 55 additions & 38 deletions
Large diffs are not rendered by default.

algorithms/qml/qsvm_pauli_feature_map/qsvm_pauli_feature_map.synthesis_options.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
"machine_precision": 8,
88
"custom_hardware_settings": {
99
"basis_gates": [
10-
"cz",
11-
"z",
10+
"p",
11+
"id",
12+
"t",
13+
"u1",
14+
"u",
1215
"rx",
16+
"cy",
1317
"x",
14-
"id",
15-
"sx",
18+
"sdg",
1619
"sxdg",
1720
"cx",
18-
"tdg",
19-
"ry",
20-
"sdg",
21-
"t",
21+
"cz",
2222
"u2",
2323
"h",
2424
"rz",
25+
"ry",
2526
"r",
26-
"u",
27-
"cy",
28-
"p",
27+
"y",
28+
"sx",
29+
"z",
2930
"s",
30-
"u1",
31-
"y"
31+
"tdg"
3232
],
3333
"is_symmetric_connectivity": true
3434
},
@@ -39,6 +39,6 @@
3939
"pretty_qasm": true,
4040
"transpilation_option": "auto optimize",
4141
"timeout_seconds": 300,
42-
"random_seed": 3890233038
42+
"random_seed": 3398128133
4343
}
4444
}

0 commit comments

Comments
 (0)