-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Optimize the HHL algorithm using amplitude amplification #7141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
I changed the example to simulate two circuits: the first one is the previous HHL circuit in the example without amplitude amplification, and the second one is the HHL circuit with amplitude amplification. The example demonstrates that amplitude amplification obtains an estimate faster (using fewer total calls to simulate.run).
I am happy to provide more explanations about the change if it helps with the review. Just let me know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to provide some tests for this class, which would help increase confidence that this large code change is working correctly.
If possible, it might be good to come to the cirq-cync on Wednesdays to discuss this so that everyone understands the changes. If not possible, we could probably discuss over GH comments.
examples/hhl.py
Outdated
cirq.measure(ancilla, key='a'), | ||
] | ||
) | ||
def __init__(self, A, C, t, register_size, *input_prep_gates): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add types for all the arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/hhl.py
Outdated
results.append(np.concatenate(results_for_param, axis=0)) | ||
runs.append(num_runs) | ||
|
||
for label, result, num_runs in zip(('X', 'Y', 'Z'), results, runs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be better and more testable if we return a result object and then print it out after it returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I moved this to a separate method print_results.
examples/hhl.py
Outdated
|
||
return circuit | ||
|
||
def simulate_without_amplification(self, total_runs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing type annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/hhl.py
Outdated
|
||
return c | ||
|
||
def measure_circuit(self, circuit): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type annotations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/hhl.py
Outdated
c.append(cirq.X.on_each(qubits)) | ||
return c | ||
|
||
def amplitude_amplification(self, num_iterations): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type annotations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thank you! I joined the cirq-dev group (my name is Codrut). Please send me the meeting invite, I'll come. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7141 +/- ##
=======================================
Coverage 98.65% 98.65%
=======================================
Files 1106 1106
Lines 95869 95939 +70
=======================================
+ Hits 94581 94651 +70
Misses 1288 1288 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Thank you for the comments! I added type annotations and created a separate method to print the results. I can add tests next. However, I don't fully understand the initial example, which prevents me from writing some unit tests. How is PhasedXPowGate used to measure X, Y and Z? When I replace the exponent parameters I don't get the matrices for Pauli X, Y or Z. Furthermore, I am not sure how the expected[] array in the example was derived. @verult it would help me a lot if you could give me some hints here. |
This addresses #2216 .
Example output from a run:
To highlight the effect of amplitude amplification, I run the algorithm twice, once without amplification, and once with, targeting the same number of estimates. Amplitude amplification reduces the number of measurements needed for an estimate (from O(1/p) to O(1/sqrt{p})). This shows up as fewer simulation runs.