-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11610d5
commit 1478b13
Showing
9 changed files
with
283 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
functions/function_usage_examples/arithmetic/modular_exp/modular_exp_example.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f6834a62-d124-48f6-bd87-d313b7eb5054", | ||
"metadata": {}, | ||
"source": [ | ||
"# Modular Exponentiation\n", | ||
"\n", | ||
"The `modular_exp` function raises a classical integer `a` to the power of a quantum number `power` modulo classical integer `n`, times a quantum number `x`.\n", | ||
"\n", | ||
"The function performs:\n", | ||
"$$|x\\rangle |power\\rangle = |(a^{power} \\mod n)\\cdot x\\rangle | power\\rangle$$\n", | ||
"\n", | ||
"Specifically if at the input $x=1$, at the output $x=a^{power} \\mod n$." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b54ad820-bdd9-4fbe-bbe7-a7d6a3db046b", | ||
"metadata": {}, | ||
"source": [ | ||
"# Example\n", | ||
"\n", | ||
"This example generates a quantum program that initializes a `power` variable with a uniform superposition, and exponentiate the classical value `A` with `power` as the exponent, in superposition. The result is calculated inplace to the variable `x` module `N`.\n", | ||
"\n", | ||
"Notice that `x` should have size of at least $\\lceil(log_2(N) \\rceil$, so it is first allocated with a fixed size, then initialized with the value '1'." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 39, | ||
"id": "6fca39ac-e769-4d85-b9a9-0bed93d2b775", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"\n", | ||
"from classiq import *\n", | ||
"from classiq.qmod.symbolic import ceiling, log\n", | ||
"\n", | ||
"# constants\n", | ||
"N = 5\n", | ||
"A = 4\n", | ||
"\n", | ||
"\n", | ||
"@qfunc\n", | ||
"def main(power: Output[QNum], x: Output[QNum]) -> None:\n", | ||
" allocate(ceiling(log(N, 2)), x)\n", | ||
" inplace_prepare_int(1, x)\n", | ||
"\n", | ||
" # initialize a uniform superposition of powers\n", | ||
" allocate(3, power)\n", | ||
" hadamard_transform(power)\n", | ||
"\n", | ||
" modular_exp(n=N, a=A, x=x, power=power)\n", | ||
"\n", | ||
"\n", | ||
"qmod = create_model(main)\n", | ||
"qmod = create_model(main, out_file=\"modular_exp_example\")\n", | ||
"qprog = synthesize(qmod)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 40, | ||
"id": "d7931d74-f1b6-4374-832e-eee4e3e0ab4b", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[{'power': 3, 'x': 4}: 136,\n", | ||
" {'power': 1, 'x': 4}: 135,\n", | ||
" {'power': 7, 'x': 4}: 130,\n", | ||
" {'power': 5, 'x': 4}: 124,\n", | ||
" {'power': 2, 'x': 1}: 124,\n", | ||
" {'power': 4, 'x': 1}: 123,\n", | ||
" {'power': 6, 'x': 1}: 117,\n", | ||
" {'power': 0, 'x': 1}: 111]" | ||
] | ||
}, | ||
"execution_count": 40, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"result = execute(qprog).result_value()\n", | ||
"result.parsed_counts" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0398be45-a152-4225-ba7b-49c1f37b197d", | ||
"metadata": {}, | ||
"source": [ | ||
"Verify all results are as expected:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 41, | ||
"id": "1363a9ae-567a-4bf1-911d-f17da9d6859c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"assert np.all(\n", | ||
" [\n", | ||
" count.state[\"x\"] == (A ** count.state[\"power\"] % N)\n", | ||
" for count in result.parsed_counts\n", | ||
" ]\n", | ||
")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
6 changes: 6 additions & 0 deletions
6
functions/function_usage_examples/arithmetic/modular_exp/modular_exp_example.metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"friendly_name": "Modular Exponentiation", | ||
"description": "Modular Exponentiation with an Exponent of Quantum Variable", | ||
"qmod_type": ["function"], | ||
"level": ["demos", "basic"] | ||
} |
7 changes: 7 additions & 0 deletions
7
functions/function_usage_examples/arithmetic/modular_exp/modular_exp_example.qmod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
qfunc main(output power: qnum, output x: qnum) { | ||
allocate(ceiling(log(5, 2)), x); | ||
inplace_prepare_int(1, x); | ||
allocate(3, power); | ||
hadamard_transform(power); | ||
modular_exp(5, 4, x, power); | ||
} |
50 changes: 50 additions & 0 deletions
50
...function_usage_examples/arithmetic/modular_exp/modular_exp_example.synthesis_options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"constraints": { | ||
"max_width": null, | ||
"max_depth": null, | ||
"max_gate_count": {}, | ||
"optimization_parameter": "no_opt" | ||
}, | ||
"preferences": { | ||
"machine_precision": 8, | ||
"backend_service_provider": null, | ||
"backend_name": null, | ||
"custom_hardware_settings": { | ||
"basis_gates": [ | ||
"cx", | ||
"rz", | ||
"t", | ||
"z", | ||
"u", | ||
"sx", | ||
"sxdg", | ||
"u2", | ||
"y", | ||
"x", | ||
"cy", | ||
"cz", | ||
"sdg", | ||
"p", | ||
"s", | ||
"id", | ||
"ry", | ||
"rx", | ||
"r", | ||
"h", | ||
"u1", | ||
"tdg" | ||
], | ||
"connectivity_map": null, | ||
"is_symmetric_connectivity": true | ||
}, | ||
"debug_mode": true, | ||
"output_format": ["qasm"], | ||
"pretty_qasm": true, | ||
"qasm3": null, | ||
"transpilation_option": "auto optimize", | ||
"solovay_kitaev_max_iterations": null, | ||
"timeout_seconds": 300, | ||
"optimization_timeout_seconds": null, | ||
"random_seed": 1209628880 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
qfunc qft(target: qbit[]) { | ||
repeat (index: target.len / 2) { | ||
SWAP(target[index], target[(target.len - 1) - index]); | ||
} | ||
qft_no_swap(target); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.