Skip to content
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

Merged
merged 21 commits into from
Nov 2, 2023

Conversation

CalMacCQ
Copy link
Collaborator

@CalMacCQ CalMacCQ commented Oct 9, 2023

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

@CalMacCQ CalMacCQ requested a review from yao-cqc October 9, 2023 13:20
@@ -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"]
Copy link
Collaborator Author

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.

Comment on lines 88 to 104
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)
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@CalMacCQ CalMacCQ Oct 10, 2023

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.

n_measurement_qubits: int, state_prep_circuit: Circuit, unitary_circuit: Circuit
) -> Circuit:
qpe_circ: Circuit = Circuit()
n_ancillas = state_prep_circuit.n_qubits
Copy link
Contributor

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.

Copy link
Collaborator Author

@CalMacCQ CalMacCQ Oct 10, 2023

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.

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 this. The use of "ancillas" throughout the notebook was actually inconsistent so thanks for pointing this out.

examples/python/phase_estimation.py Outdated Show resolved Hide resolved
Comment on lines 436 to 446
# 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)
Copy link
Contributor

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?

Copy link
Collaborator Author

@CalMacCQ CalMacCQ Oct 10, 2023

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

$e^{i \frac{\pi}{2}\theta YXXX}|1100\rangle$

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.


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)
Copy link
Contributor

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.

Copy link
Collaborator Author

@CalMacCQ CalMacCQ Oct 10, 2023

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.

from pytket.passes import DecomposeBoxes


DecomposeBoxes().apply(ham_circ)
Copy link
Contributor

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.

@CalMacCQ CalMacCQ changed the title Examples/redo qpe Redo example notebook on Quantum Phase Estimation (QPE) Oct 11, 2023
@CalMacCQ CalMacCQ requested a review from yao-cqc November 2, 2023 15:24
@CalMacCQ CalMacCQ merged commit 0839eb8 into main Nov 2, 2023
5 checks passed
@CalMacCQ CalMacCQ deleted the examples/redo_qpe branch November 2, 2023 15:30
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