Skip to content

Product/matanvax2/cls 5625 beautify qmods#1440

Merged
matanvax2 merged 7 commits intomainfrom
Product/matanvax2/CLS-5625-beautify-qmods
Jan 20, 2026
Merged

Product/matanvax2/cls 5625 beautify qmods#1440
matanvax2 merged 7 commits intomainfrom
Product/matanvax2/CLS-5625-beautify-qmods

Conversation

@matanvax2
Copy link
Member

PR Description

Beautify qmods - my part

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

state_loading(state[1:state.len], state[0]);
allocate(phase);
qpe(lambda() {
my_grover_operator(state);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not changing to the regular grover_operator?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess the reason is the intent of this example, to demonstrate the QCM algorithm in full. I would ask the same question about the notebook first..

// eigenvalues of a given matrix by applying an exactly exponentiated unitary
// operator.

unitary_for_HM: real[][] = [ // the unitary matrix obtained by exp(i·2π·HM)
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is HM? it is not defined

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

// operator.

unitary_for_HM: real[][] = [ // the unitary matrix obtained by exp(i·2π·HM)
[
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can write each row of the matrix in a single row

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure it's better - it yields lines with 170 characters, which requires right-scrolling and is typically not recommended.

// of the 3-SAT problem, showing how quantum amplitude amplification can be
// used to find a satisfying assignment.

qperm sat3_oracle(const x: qbit[], res: qbit) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

sat3 -> 3sat

Copy link
Member Author

Choose a reason for hiding this comment

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

Identifiers that start with numeric characters are illegal in Qmod (and in all programming languages I know).

allocate(x);
hadamard_transform(x);
power (r) {
grover_operator(lambda(vars) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would define 3sat_oracle directly as a phase oracle using controlled phase(pi), then provide it grover_operator

Copy link
Member Author

Choose a reason for hiding this comment

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

I would love to, here and in the notebook too! But at the moment it yields 2X in depth (because uncomp-recomp). This degradation will be removed with the migration to the new arithmetic, hopefully soon. Do you (and your boss) approve this temporary degradation?

@@ -0,0 +1,33 @@
// Solving 3-SAT Problem using Grover's Algorithm
Copy link
Collaborator

Choose a reason for hiding this comment

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

rename to 3_sat_grover (without the large)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

qfunc main(params: real[6], output v: KnapsackVars) {
allocate(v);
hadamard_transform(v);
repeat (i: 3) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

change to num_layers

Copy link
Member Author

Choose a reason for hiding this comment

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

See reply to the next comment..

}
}

qfunc main(params: real[6], output v: KnapsackVars) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

change 6 to 2*num_layers

Copy link
Member Author

Choose a reason for hiding this comment

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

I would love to, but alas -
https://classiq.atlassian.net/browse/CLS-5663

// This function outputs the segment label of a given x value
qperm map_label(num_segs: int, const v: qbit[], output label: qbit[]) {
allocate(num_segs, label);
repeat (i: num_segs) { // duplicate MSBs of v
Copy link
Collaborator

Choose a reason for hiding this comment

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

map_label can use |= between QArrays instead of the repeat

@@ -15,7 +18,7 @@ qfunc compute_piecewise_linear(As: real[], Bs: real[], x: qnum, y: qnum) {

repeat (i: As.len) {
control (label==i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of copying the label, now you can control operation with constant x based on x value, so you can save the auxilliary qubits

@matanvax2 matanvax2 force-pushed the Product/matanvax2/CLS-5625-beautify-qmods branch from f81878a to 41f03a9 Compare January 20, 2026 09:05
@matanvax2 matanvax2 force-pushed the Product/matanvax2/CLS-5625-beautify-qmods branch from 41f03a9 to 18c60f6 Compare January 20, 2026 09:13
@matanvax2 matanvax2 merged commit d3edc75 into main Jan 20, 2026
6 checks passed
@matanvax2 matanvax2 deleted the Product/matanvax2/CLS-5625-beautify-qmods branch January 20, 2026 12:06
@github-actions
Copy link

💪 Great job, @matanvax2! You've merged your 8th PR! 🎊

Your contributions to classiq-library are making a real difference. Keep up the fantastic work! 💪
Remember, every PR counts and helps improve the project. What will you tackle next? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants