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

Random walk phase estimation example in Python #1579 #2060

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/sphinx/examples/python/random_walk_qpe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Compile and run with:
# ```
# nvq++ random_walk_qpe.cpp -o qpe.x && ./qpe.x
# ```

import cudaq
from typing import List
from cudaq import spin

# Here we demonstrate an algorithm expressed as a CUDA-Q kernel
# that incorporates non-trivial control flow and conditional
# quantum instruction invocation.


# Define the random walk phase estimation kernel
@cudaq.kernel
def rwpe_kernel(n_iter: int, mu: float, sigma: float) -> float:
iteration = 0

# Allocate the qubits
number_of_qubits = 2
qubits = cudaq.qvector(number_of_qubits)

# Alias them
aux = qubits[0]

target = qubits[number_of_qubits - 1]

x(target)

while iteration < n_iter:
h(aux)
rz(1.0 - (mu / sigma), aux)
rz(.25 / sigma, target)
x.ctrl(aux, target)
rz(-.25 / sigma, target)
x.ctrl(aux, target)
h(aux)
if mz(aux):
x(aux)
mu = mu + sigma * .6065
else:
mu = mu - sigma * .6065

sigma *= .7951
iteration += 1

return 2.0 * mu


# Main function to execute the kernel
def main():
n_iterations = 24
mu = 0.7951
sigma = 0.6065

phase = rwpe_kernel(n_iterations, mu, sigma)
print(f"Phase = {phase:.6f}")


if __name__ == "__main__":
main()
Loading