Skip to content

Commit

Permalink
use qintimp instead of qint, update test and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Mar 18, 2024
1 parent d3a0c1a commit fc52e0a
Show file tree
Hide file tree
Showing 41 changed files with 394 additions and 299 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pip install qlasskit
For a quickstart, read the _quickstart_ and _examples_ notebooks from the documentation: [https://dakk.github.io/qlasskit](https://dakk.github.io/qlasskit).

```python
from qlasskit import qlassf, Qint4
from qlasskit import qlassf, Qint

@qlassf
def h(k: Qint4) -> bool:
def h(k: Qint[4]) -> bool:
h = True
for i in range(4):
h = h and k[i]
Expand Down Expand Up @@ -65,11 +65,11 @@ You can also use other functions inside a qlassf:

```python
@qlassf
def equal_8(n: Qint4) -> bool:
def equal_8(n: Qint[4]) -> bool:
return equal_8 == 8

@qlassfa(defs=[equal_8])
def f(n: Qint4) -> bool:
def f(n: Qint[4]) -> bool:
n = n+1 if equal_8(n) else n
return n
```
Expand All @@ -78,13 +78,13 @@ Qlasskit supports complex data types, like tuples and fixed size lists:

```python
@qlassf
def f(a: Tuple[Qint8, Qint8]) -> Tuple[bool, bool]:
def f(a: Tuple[Qint[8], Qint[8]]) -> Tuple[bool, bool]:
return a[0] == 42, a[1] == 0
```

```python
@qlassf
def search(alist: Qlist[Qint2, 4], to_search: Qint2):
def search(alist: Qlist[Qint[2], 4], to_search: Qint[2]):
for x in alist:
if x == to_search:
return True
Expand Down
22 changes: 11 additions & 11 deletions docs/source/algorithms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -41,18 +41,18 @@
"4"
]
},
"execution_count": 19,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import Tuple\n",
"from qlasskit import qlassf, Qint4\n",
"from qlasskit import qlassf, Qint, Qint4\n",
"\n",
"\n",
"@qlassf\n",
"def test_tools(a: Qint4) -> Qint4:\n",
"def test_tools(a: Qint[4]) -> Qint[4]:\n",
" return a + 1\n",
"\n",
"\n",
Expand All @@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -77,7 +77,7 @@
"'0100'"
]
},
"execution_count": 18,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -95,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -133,15 +133,15 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from qlasskit.algorithms import Simon\n",
"\n",
"\n",
"@qlassf\n",
"def f(a: Qint4) -> Qint4:\n",
"def f(a: Qint[4]) -> Qint[4]:\n",
" return (a >> 3) + 1\n",
"\n",
"\n",
Expand All @@ -167,15 +167,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from qlasskit.algorithms import DeutschJozsa\n",
"\n",
"\n",
"@qlassf\n",
"def f(b: Qint4) -> bool:\n",
"def f(b: Qint[4]) -> bool:\n",
" return b < 16\n",
"\n",
"\n",
Expand Down
33 changes: 26 additions & 7 deletions docs/source/bqm.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/source/example_bqm_polynomial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
}
],
"source": [
"from qlasskit import qlassf, qlassfa, Qmatrix, Qint2, Qint3, Qint4, Qfixed\n",
"from qlasskit.types.qfixed import Qfixed2_4\n",
"from qlasskit import qlassf, Qfixed\n",
"\n",
"\n",
"@qlassf\n",
"def poly(x: Qfixed[3, 3], y: Qfixed[3, 3]) -> Qfixed[3, 3]:\n",
" return x * 3 - y * 2 + 1\n",
"\n",
"\n",
"poly.expressions"
]
},
Expand All @@ -70,14 +70,14 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'x': 7.875, 'y': 3.375} DecodedSample(-640.0, {'x': 7.875, 'y': 3.375})\n"
"{'x': 7.875, 'y': 7.375}\n"
]
}
],
Expand Down
28 changes: 15 additions & 13 deletions docs/source/example_bqm_tsp.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/source/example_deutsch_jozsa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"metadata": {},
"outputs": [],
"source": [
"from qlasskit import qlassf, Qint4\n",
"from qlasskit import qlassf, Qint\n",
"\n",
"\n",
"@qlassf\n",
"def f(a: Qint4) -> bool:\n",
"def f(a: Qint[4]) -> bool:\n",
" return a > 7"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/source/example_grover.ipynb

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/source/example_grover_factors.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/source/example_grover_hash.ipynb

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/source/example_grover_subset.ipynb

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/source/example_grover_sudoku.ipynb

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions docs/source/example_simon.ipynb

Large diffs are not rendered by default.

17 changes: 3 additions & 14 deletions docs/source/exporter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"metadata": {},
"outputs": [],
"source": [
"from qlasskit import Qint2, qlassf\n",
"from qlasskit import Qint, qlassf\n",
"\n",
"\n",
"@qlassf\n",
"def hello_world(a: bool, b: Qint2) -> Qint2:\n",
"def hello_world(a: bool, b: Qint[2]) -> Qint[2]:\n",
" return b + (1 if a else 0)"
]
},
Expand Down Expand Up @@ -213,18 +213,7 @@
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Gate(hello_world, targets=[0, 1, 2, 3, 4], controls=None, classical controls=None, control_value=None, classical_control_value=None)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# Disabled on docs for a depencency problem\n",
"# qc = hello_world.export(\"qutip\")\n",
Expand Down
8 changes: 4 additions & 4 deletions docs/source/how_it_works.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"metadata": {},
"outputs": [],
"source": [
"from qlasskit import qlassf, Qint2, Qint4\n",
"from qlasskit import qlassf\n",
"from qiskit import QuantumCircuit\n",
"\n",
"\n",
"@qlassf\n",
"def f_comp(b: bool, n: Qint2) -> Qint2:\n",
"def f_comp(b: bool, n: Qint[2]) -> Qint[2]:\n",
" for i in range(3):\n",
" n += 1 if b else 2\n",
" return n"
Expand Down Expand Up @@ -72,7 +72,7 @@
],
"source": [
"@qlassf\n",
"def f1(b: bool, n: Qint2) -> Qint2:\n",
"def f1(b: bool, n: Qint[2]) -> Qint[2]:\n",
" return n + (1 if b else 2)\n",
"\n",
"\n",
Expand Down Expand Up @@ -151,7 +151,7 @@
"outputs": [],
"source": [
"@qlassf\n",
"def f(n: Qint4) -> bool:\n",
"def f(n: Qint[4]) -> bool:\n",
" return n == 3"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/source/parameters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
}
],
"source": [
"from qlasskit import Qlist, Qint2, Qint4\n",
"from qlasskit import Qlist, Qint, Qint4\n",
"\n",
"\n",
"@qlassf\n",
"def test(a: Parameter[Qlist[Qint2, 4]], b: Qint4) -> Qint4:\n",
"def test(a: Parameter[Qlist[Qint[2], 4]], b: Qint[4]) -> Qint[4]:\n",
" s = Qint4(0)\n",
" for n in a:\n",
" s += n\n",
Expand Down
20 changes: 14 additions & 6 deletions docs/source/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"metadata": {},
"outputs": [],
"source": [
"from qlasskit import qlassf, Qint2\n",
"from qlasskit import qlassf, Qint, Qint2\n",
"\n",
"\n",
"@qlassf\n",
"def sum_two_numbers(a: Qint2, b: Qint2) -> Qint2:\n",
"def sum_two_numbers(a: Qint[2], b: Qint[2]) -> Qint[2]:\n",
" return a + b"
]
},
Expand Down Expand Up @@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -75,7 +75,7 @@
"<Figure size 789.163x618.722 with 1 Axes>"
]
},
"execution_count": 9,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -95,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -105,7 +105,7 @@
"<Figure size 700x500 with 1 Axes>"
]
},
"execution_count": 10,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -136,7 +136,15 @@
"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.10.13"
}
},
Expand Down
11 changes: 6 additions & 5 deletions docs/source/supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ Boolean type.
Qint
^^^^

Unsigned integers; this type has subtypes for different Qint sizes (Qint2, Qint4, Qint8, Qint12, Qint16).
Unsigned integers; `Qint[2]` has 2 bits, and there other sizes are supported.
Single bit of the Qint are accessible by the subscript operator `[]`.

All the supported sizes have a constructor `Qintn()` defined in `qlasskit.types`.

Qfixed
^^^^^^

Fixed point rational number; `Qfixed[2,3]` has 2 bits for the integer part and 3 bits for the fractional.
All the supported sizes have a constructor `Qfixedn_m()` defined in `qlasskit.types`.

Qchar
^^^^^
Expand All @@ -53,14 +54,14 @@ List
^^^^

Qlist[T, size] denotes a fixed-size list in qlasskit.
For example, the list `[1,2,3]` is typed as `Qlist[Qint2,3]`.
For example, the list `[1,2,3]` is typed as `Qlist[Qint[2],3]`.


Matrix
^^^^

Qmatrix[T, m, n] denotes a fixed-size list in qlasskit.
For example, the matrix `[[1,2],[3,4]]` is typed as `Qmatrix[Qint2,2,2]`.
For example, the matrix `[[1,2],[3,4]]` is typed as `Qmatrix[Qint[2],2,2]`.



Expand Down Expand Up @@ -243,7 +244,7 @@ Function def

.. code-block:: python
def f(t: Qlist[Qint4,2]) -> Qint4:
def f(t: Qlist[Qint[4],2]) -> Qint[4]:
return t[0] + t[1]
Expand Down
5 changes: 4 additions & 1 deletion qlasskit/ast2logic/t_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def to_name(a):
if isinstance(a, ast.Attribute):
return a.attr
elif isinstance(a, ast.Subscript): # for Qfixed and similar
t = "_".join(f"{e.value}" for e in a.slice.elts)
if isinstance(a.slice, ast.Constant):
t = f"{a.slice.value}"
else:
t = "_".join(f"{e.value}" for e in a.slice.elts)
return f"{a.value.id}{t}"
else:
return a.id
Expand Down
Loading

0 comments on commit fc52e0a

Please sign in to comment.