-
Notifications
You must be signed in to change notification settings - Fork 88
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
Redo example notebook on Quantum Phase Estimation (QPE) #262
Conversation
@@ -11,7 +11,7 @@ sphinx: | |||
|
|||
execute: | |||
# Exclude some examples from execution (these are still deployed as html pages) | |||
exclude_patterns: [".venv/*", "Boxes_QPE_example.ipynb", "Forest_portability_example.ipynb", "backends_example.ipynb", "qiskit_integration.ipynb", "comparing_simulators.ipynb", "expectation_value_example.ipynb", "pytket-qujax_heisenberg_vqe.ipynb", "spam_example.ipynb", "tket_benchmarking.ipynb", "entanglement_swapping.ipynb", "pytket-qujax-classification.ipynb"] |
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.
The commit message "Execute QPE notebook" is misleading here. This was the name of an old file from a previous PR.
The QPE notebook is executed on CI.
examples/python/phase_estimation.py
Outdated
qft3_circ = Circuit(3) | ||
|
||
qft3_circ.H(0) | ||
qft3_circ.CU1(0.5, 1, 0) | ||
qft3_circ.CU1(0.25, 2, 0) | ||
|
||
qft3_circ.H(1) | ||
qft3_circ.CU1(0.5, 2, 1) | ||
|
||
qft3_circ.H(2) | ||
|
||
qft3_circ.SWAP(0, 2) | ||
|
||
|
||
from pytket.circuit.display import render_circuit_jupyter | ||
|
||
render_circuit_jupyter(qft3_circ) |
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.
Do all of these need to be in separate cells? There are also other places like this.
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.
Probably not tbh. Will make them the same cell.
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.
Ah in fact this was not intended by me.
It is caused by the way the gen-nb
script generates notebooks from python files. The p2j
library seems to interpret these new lines as separate cells.
I'll see if I can work around this without squashing everything together.
examples/python/phase_estimation.py
Outdated
n_measurement_qubits: int, state_prep_circuit: Circuit, unitary_circuit: Circuit | ||
) -> Circuit: | ||
qpe_circ: Circuit = Circuit() | ||
n_ancillas = state_prep_circuit.n_qubits |
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.
Is it common to call these ancillas? It's a little bit odd to me.
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 don't like it either. I think I'll go with n_prep_qubits
instead.
Thanks for pointing this out. When I've done QPE in other places I've called them prep qubits instead.
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.
fixed this. The use of "ancillas" throughout the notebook was actually inconsistent so thanks for pointing this out.
examples/python/phase_estimation.py
Outdated
# we can extract the statevector for $e^{i \frac{\pi}{2}\theta YXXX}|1100\rangle$ using the `Circuit.get_statvector()` method. | ||
|
||
|
||
initial_state = state_circ.get_statevector() | ||
|
||
# Finally we can prepare our initial state using `StatePreparationBox`. | ||
|
||
|
||
from pytket.circuit import StatePreparationBox | ||
|
||
state_prep_box = StatePreparationBox(initial_state) |
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.
Why can't we just use the state_circ
constructed above to prepare the initial state?
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 wasn't that happy with this state preparation example using the
state.
Phase estimation seemed like a nice way to include QControlBox
, CircBox
, PauliExpBox
, StatePreparationBox
(and perhaps ConjugationBox
) all in the same demo.
Maybe I should ask someone from chemistry if they can give me an initial state which is a better fit for StatePreparationBox
.
Currently it feels a bit artifical.
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 what I'm going to do here is just get rid of StatePreparationBox
from this notebook and do the state prep with PauliExpBox
.
I was just trying to squeeze all boxes into one notebook but PauliExpBox
would be way better in this case anyway.
I'll find another way to demo StatePreparationBox
in another notebook perhaps.
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've removed the use of StatePreparationBox
.
Just have to fix the simulation results now.
examples/python/phase_estimation.py
Outdated
|
||
ham_result = backend.run_circuit(compiled_ham_circ, n_shots=n_shots) | ||
|
||
plot_qpe_results(ham_result, y_limit=int(1.2 * n_shots), n_strings=5) |
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.
Can you explain the results a little bit? I think it's quite important to explain what to do if the initial state is not a eigenstate.
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.
Yeah I should explain the output a bit more.
I think it may be worth just redoing the last example once I've found a better choice of state prep circuit.
examples/python/phase_estimation.py
Outdated
from pytket.passes import DecomposeBoxes | ||
|
||
|
||
DecomposeBoxes().apply(ham_circ) |
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.
Keep in mind that the next pytket release will wrap some components of ham_circ
in ConjugationBoxes
, so the subsequent QControlBox
will optimise for these boxes. Calling DecomposeBoxes
here will remove the ConjugationBoxes
.
Redoing #241 as I ran into a bunch of issues with source control.
Notebook example for Quantum phase estimation based party on ->
https://quantumcomputing.stackexchange.com/questions/32594/how-would-you-draw-the-phase-estimation-circuit-for-the-eigenvalues-of-u-mat/32598#32598