Skip to content
Closed

Qsvm #1418

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
953 changes: 825 additions & 128 deletions algorithms/qml/qsvm/qsvm.ipynb

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions algorithms/qml/qsvm/qsvm.qmod

This file was deleted.

16 changes: 16 additions & 0 deletions algorithms/qml/qsvm/qsvm_bloch_sphere_feature_map.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
qfunc bloch_feature_map_expanded___0(data: real[2], qba: qbit[1]) {
repeat (i: 1) {
RX(data[2 * i] / 2, qba[i]);
}
repeat (i: 1) {
RZ(data[(2 * i) + 1], qba[i]);
}
}

qfunc main(data1: real[2], data2: real[2], output qba: qnum<1, False, 0>) {
allocate(1, qba);
bloch_feature_map_expanded___0(data1, qba);
invert {
bloch_feature_map_expanded___0(data2, qba);
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"custom_hardware_settings": {
"basis_gates": [
"t",
"u1",
"u2",
"z",
"rx",
"cz",
"sx",
"t",
"u",
"id",
"rz",
"rx",
"cx",
"tdg",
"sdg",
"p",
"z",
"sxdg",
"ry",
"h",
"y",
"sxdg",
"sx",
"cy",
"u",
"r",
"u1",
"sdg",
"s",
"u2",
"tdg",
"id",
"ry",
"x",
"cx",
"y"
"r",
"s"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"machine_precision": 8,
"optimization_level": 3,
"optimization_level": 1,
"output_format": ["qasm"],
"pretty_qasm": true,
"random_seed": 1598682726,
"random_seed": 2336470090,
"synthesize_all_separately": false,
"timeout_seconds": 300,
"transpilation_option": "auto optimize"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"custom_hardware_settings": {
"basis_gates": [
"sdg",
"cx",
"u1",
"u2",
"z",
"rx",
"cz",
"t",
"ry",
"rz",
"u",
"id",
"rz",
"cx",
"tdg",
"y",
"sdg",
"p",
"ry",
"h",
"u1",
"y",
"sxdg",
"sx",
"s",
"cz",
"x",
"p",
"cy",
"rx",
"sxdg",
"id",
"x",
"r",
"u2",
"z"
"s"
],
"is_symmetric_connectivity": true
},
Expand All @@ -35,7 +36,7 @@
"optimization_level": 1,
"output_format": ["qasm"],
"pretty_qasm": true,
"random_seed": 3113768414,
"random_seed": 3912696462,
"synthesize_all_separately": false,
"timeout_seconds": 300,
"transpilation_option": "auto optimize"
Expand Down
600 changes: 0 additions & 600 deletions algorithms/qml/qsvm_pauli_feature_map/qsvm_pauli_feature_map.ipynb

This file was deleted.

This file was deleted.

19 changes: 16 additions & 3 deletions tests/notebooks/test_qsvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@
from testbook.client import TestbookNotebookClient


@wrap_testbook("qsvm", timeout_seconds=204)
@wrap_testbook("qsvm", timeout_seconds=300)
def test_notebook(tb: TestbookNotebookClient) -> None:
"""
A notebook for a hybrid classical quantum neural network.
The test verifies that the pre-trained model is indeed well trained.
"""
# test models
validate_quantum_model(tb.ref("QSVM_BLOCH_SHPERE_qmod"))
validate_quantum_model(tb.ref("QSVM_PAULI_Z_ZZ"))
# test quantum programs
validate_quantum_program_size(
tb.ref_pydantic("qprog"),
tb.ref_pydantic("qprog_bloch"),
expected_width=1, # actual width: 1
expected_depth=10, # actual depth: 4
)

validate_quantum_program_size(
tb.ref_pydantic("qprog_pauli"),
expected_width=2, # actual width: 2
expected_depth=30, # actual depth: 30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe take some buffer fo the depth here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks!

)
# test notebook content
assert tb.ref("test_score") == 1

# test notebook content
assert tb.ref("test_score_pauli") == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, and in the line before (which you did not modify), maybe change this to 1.0? or even to <0.98?
In case there will be some fluctuations from time to time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


success_rate = tb.ref(
"100 * np.count_nonzero(predicted_labels_pauli == predict_real_labels_2) / len(predicted_labels_pauli)"
)
assert success_rate == 100
30 changes: 0 additions & 30 deletions tests/notebooks/test_qsvm_pauli_feature_map.py

This file was deleted.