Skip to content

Commit 279412a

Browse files
revised q counting iqae qmod
1 parent 5ef70a5 commit 279412a

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed
Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,72 @@
1+
// Quantum Counting Using Iterative Quantum Amplitude Estimation
2+
3+
// Prepares the IQAE input state over the problem variables (a,b) and an indicator qubit.
4+
// The quantum variables are transformed into uniform superposition, and the indicator is computed as:
5+
// ind = 1 iff (a + b) <= THRESHOLD.
6+
// In the IQAE workflow, the indicator qubit defines the “good” subspace; a phase flip (Z) on
7+
// this qubit implements the marking oracle used inside amplitude amplification iterations.
8+
9+
10+
// Defining the QStruct, containing the quantum variables a and b
111
qstruct OracleVars {
212
a: qnum<2>;
313
b: qnum<2>;
414
}
515

6-
qperm oracle(const est_reg: qbit[]) {
7-
Z(est_reg[est_reg.len - 1]);
16+
17+
// Configuration constants
18+
A_BITS: int = 2;
19+
B_BITS: int = 2;
20+
21+
PROBLEM_QUBITS: int = A_BITS + B_BITS; // 4 qubits: a[2] || b[2]
22+
THRESHOLD: int = 2; // predicate: (a + b) <= THRESHOLD
23+
24+
25+
26+
// Phase oracle: flips the phase of a marked states.
27+
// Here we mark states by applying Z on the indicator qubit (assumed last qubit in est_var).
28+
qperm oracle(const est_var: qbit[]) {
29+
Z(est_var[est_var.len - 1]);
830
}
931

32+
33+
// Computes the boolean predicate into `res` via XOR assignment.
34+
// `res` becomes 1 iff (a + b) <= THRESHOLD (assuming it starts at |0⟩).
1035
qperm arith_equation(const state: OracleVars, res: qbit) {
11-
res ^= (state.a + state.b) <= 2;
36+
res ^= (state.a + state.b) <= THRESHOLD;
1237
}
1338

39+
// State preparation for amplitude amplification:
1440
qfunc iqae_state_preparation(vars: OracleVars, ind: qbit) {
15-
hadamard_transform(vars);
16-
arith_equation(vars, ind);
41+
hadamard_transform(vars); // Uniform superposition over all possible states
42+
arith_equation(vars, ind); // Compute predicate into indicator
1743
}
1844

19-
qfunc space_transform(est_reg: qbit[]) {
20-
iqae_state_preparation(est_reg[0:est_reg.len - 1], est_reg[est_reg.len - 1]);
45+
// Constructs the state expected by `amplitude_amplification`:
46+
// interprets the first PROBLEM_QUBITS as OracleVars and the last qubit as an indicator.
47+
qfunc space_transform(est_var: qbit[]) {
48+
// est_var = [problem_vars..., indicator]
49+
iqae_state_preparation(
50+
est_var[0:est_var.len - 1], // OracleVars view of the problem qubits
51+
est_var[est_var.len - 1] // Indicator qubit
52+
);
2153
}
2254

55+
2356
qfunc main(k: int, output indicator: qbit) {
24-
est_reg: qbit[];
25-
problem_vars: qbit[4];
26-
allocate(problem_vars);
27-
allocate(indicator);
57+
est_var: qbit[];
58+
problem_vars: qbit[PROBLEM_QUBITS];
59+
60+
allocate(problem_vars); // Initiating the indicator qubit
61+
allocate(indicator); // Solution space qubits
62+
63+
// Create a quantum variable for amplitude amplification.
2864
within {
29-
{problem_vars, indicator} -> est_reg;
65+
{problem_vars, indicator} -> est_var;
3066
} apply {
31-
amplitude_amplification(k, oracle, space_transform, est_reg);
67+
// Apply k iterations of amplitude amplification using the oracle and space transform.
68+
amplitude_amplification(k, oracle, space_transform, est_var);
3269
}
33-
drop(problem_vars);
70+
71+
drop(problem_vars); // Indicates that the quantum state should not be measured
3472
}

0 commit comments

Comments
 (0)