Skip to content

Commit

Permalink
Merge branch 'Classiq:main' into trial_1
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiloMathysicist authored Jun 8, 2024
2 parents d366017 + f7386d5 commit 3f72068
Show file tree
Hide file tree
Showing 25 changed files with 1,670 additions and 40 deletions.
11 changes: 11 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### PR Description

<!-- Describe the PR's general purpose -->

### Some notes

- [ ] Please make sure that you placed the files in an appropriate folder
- [ ] And that the files have indicative names.

- [ ] Please note that Classiq runs automatic code linting, which may minorly alter some files.
- [ ] If you're familiar with `pre-commit`, you may run `pre-commit install`, and then at each commit, your files will be altered in a similar way
23 changes: 23 additions & 0 deletions .github/workflows/Lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint

on: [push, workflow_dispatch]

permissions:
contents: read

env:
FORCE_COLOR: 1
RUFF_OUTPUT_FORMAT: github

jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- uses: pre-commit/action@v3.0.1
if: false
48 changes: 48 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Test notebooks"

on: [pull_request, workflow_dispatch]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: "Install dependencies"
run: |
python -m pip install -U -r requirements.txt
python -m pip install -U -r requirements_tests.txt
- name: Get changed files - all
id: changed-files-all
uses: tj-actions/changed-files@v44
- name: Get changed files - ipynb
id: changed-files-ipynb
uses: tj-actions/changed-files@v44
with:
files: |
**.ipynb
- name: Set environment variables
run: |
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "SHOULD_TEST_ALL_FILES=false" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=${{ steps.changed-files-all.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=${{ steps.changed-files-all.outputs.all_changed_files }}" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.any_changed }}" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=${{ steps.changed-files-ipynb.outputs.all_changed_files }}" >> $GITHUB_ENV
elif [ "${{ github.event_name }}" == 'workflow_dispatch' ]; then
echo "SHOULD_TEST_ALL_FILES=true" >> $GITHUB_ENV
echo "HAS_ANY_FILE_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_FILE_CHANGED=None" >> $GITHUB_ENV
echo "HAS_ANY_IPYNB_CHANGED=None" >> $GITHUB_ENV
echo "LIST_OF_IPYNB_CHANGED=None" >> $GITHUB_ENV
fi
- name: "Run tests"
run: python -m pytest tests
env:
JUPYTER_PLATFORM_DIRS: "1"
36 changes: 18 additions & 18 deletions algorithms/algebraic/discrete_log/discrete_log.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"id": "2bbc4aa3-8f61-433d-a1cd-c58df445a2fd",
"metadata": {
"execution": {
Expand All @@ -92,20 +92,20 @@
"\n",
"@qfunc\n",
"def discrete_log_oracle(\n",
" g: CInt,\n",
" x: CInt,\n",
" N: CInt,\n",
" g_generator: CInt,\n",
" x_element: CInt,\n",
" N_modulus: CInt,\n",
" order: CInt,\n",
" x1: QArray[QBit],\n",
" x2: QArray[QBit],\n",
" func_res: Output[QArray[QBit]],\n",
") -> None:\n",
"\n",
" allocate(ceiling(log(N, 2)), func_res)\n",
" allocate(ceiling(log(N_modulus, 2)), func_res)\n",
"\n",
" inplace_prepare_int(1, func_res)\n",
" modular_exp(N, x, func_res, x1)\n",
" modular_exp(N, g, func_res, x2)"
" modular_exp(N_modulus, x_element, func_res, x1)\n",
" modular_exp(N_modulus, g_generator, func_res, x2)"
]
},
{
Expand All @@ -122,7 +122,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"id": "d7fb63ac-023d-40ac-9395-13c1274d446f",
"metadata": {
"execution": {
Expand All @@ -140,9 +140,9 @@
"\n",
"@qfunc\n",
"def discrete_log(\n",
" g: CInt,\n",
" x: CInt,\n",
" N: CInt,\n",
" g_generator: CInt,\n",
" x_element: CInt,\n",
" N_modulus: CInt,\n",
" order: CInt,\n",
" x1: Output[QArray[QBit]],\n",
" x2: Output[QArray[QBit]],\n",
Expand All @@ -155,7 +155,7 @@
" hadamard_transform(x1)\n",
" hadamard_transform(x2)\n",
"\n",
" discrete_log_oracle(g, x, N, order, x1, x2, func_res)\n",
" discrete_log_oracle(g_generator, x_element, N_modulus, order, x1, x2, func_res)\n",
"\n",
" invert(lambda: qft(x1))\n",
" invert(lambda: qft(x2))"
Expand Down Expand Up @@ -209,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"id": "8ef24bb4-b063-42d6-a5ea-ddcd5310ae13",
"metadata": {
"execution": {
Expand All @@ -224,8 +224,8 @@
"from classiq import Constraints, create_model\n",
"\n",
"MODULU_NUM = 5\n",
"G = 3\n",
"X = 2\n",
"G_GENERATOR = 3\n",
"X_LOGARITHM = 2\n",
"ORDER = MODULU_NUM - 1 # as 5 is prime\n",
"\n",
"\n",
Expand All @@ -235,12 +235,12 @@
" x2: Output[QNum],\n",
" func_res: Output[QNum],\n",
") -> None:\n",
" discrete_log(G, X, MODULU_NUM, ORDER, x1, x2, func_res)"
" discrete_log(G_GENERATOR, X_LOGARITHM, MODULU_NUM, ORDER, x1, x2, func_res)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"id": "8e9d0b08-f3f8-4e95-a468-bfeec010d97c",
"metadata": {
"execution": {
Expand Down Expand Up @@ -272,7 +272,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "0aef2760-aa9a-4710-8c60-08333a0a9b0b",
"metadata": {
"execution": {
Expand Down
13 changes: 7 additions & 6 deletions algorithms/algebraic/discrete_log/discrete_log.qmod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
qfunc discrete_log_oracle<g: int, x: int, N: int, order: int>(x1: qbit[], x2: qbit[], output func_res: qbit[]) {
allocate<ceiling(log(N, 2))>(func_res);
qfunc discrete_log_oracle<g_generator: int, x_element: int, N_modulus: int, order: int>(x1: qbit[], x2: qbit[], output func_res: qbit[]) {
allocate<ceiling(log(N_modulus, 2))>(func_res);
inplace_prepare_int<1>(func_res);
modular_exp<N, x>(func_res, x1);
modular_exp<N, g>(func_res, x2);
modular_exp<N_modulus, x_element>(func_res, x1);
modular_exp<N_modulus, g_generator>(func_res, x2);
}

qfunc discrete_log<g: int, x: int, N: int, order: int>(output x1: qbit[], output x2: qbit[], output func_res: qbit[]) {
qfunc discrete_log<g_generator: int, x_element: int, N_modulus: int, order: int>(output x1: qbit[], output x2: qbit[], output func_res: qbit[]) {
allocate<ceiling(log(order, 2))>(x1);
allocate<ceiling(log(order, 2))>(x2);
hadamard_transform(x1);
hadamard_transform(x2);
discrete_log_oracle<g, x, N, order>(x1, x2, func_res);
discrete_log_oracle<g_generator, x_element, N_modulus, order>(x1, x2, func_res);
invert {
qft(x1);
}
Expand All @@ -22,3 +22,4 @@ qfunc discrete_log<g: int, x: int, N: int, order: int>(output x1: qbit[], output
qfunc main(output x1: qnum, output x2: qnum, output func_res: qnum) {
discrete_log<3, 2, 5, 4>(x1, x2, func_res);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
qfunc my_mcx(ctrl: qbit[], target: qbit) {
control (ctrl) {
X(target);
}
}

qfunc main(output ctrl: qbit[], output target: qbit) {
allocate<12>(ctrl);
allocate<1>(target);
my_mcx(ctrl, target);
}

Loading

0 comments on commit 3f72068

Please sign in to comment.