Skip to content

Commit

Permalink
Merge branch 'quantumlib:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Feb 4, 2025
2 parents a901d54 + 1d9ecc7 commit 7b3aef9
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 89 deletions.
19 changes: 17 additions & 2 deletions qualtran/_infra/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import abc
from enum import Enum
from functools import cached_property
from typing import Any, Iterable, List, Sequence, Union
from typing import Any, Iterable, List, Optional, Sequence, TYPE_CHECKING, Union

import attrs
import numpy as np
Expand All @@ -60,6 +60,9 @@

from qualtran.symbolics import bit_length, is_symbolic, SymbolicInt

if TYPE_CHECKING:
import galois


class QDType(metaclass=abc.ABCMeta):
"""This defines the abstract interface for quantum data types."""
Expand Down Expand Up @@ -888,6 +891,10 @@ class QGF(QDType):
characteristic: The characteristic $p$ of the field $GF(p^m)$.
The characteristic must be prime.
degree: The degree $m$ of the field $GF(p^{m})$. The degree must be a positive integer.
irreducible_poly: Optional galois.Poly instance that defines the field arithmetic.
This parameter is passed to `galois.GF(..., irreducible_poly=irreducible_poly)`.
element_repr: The string representation of the galois elements.
This parameter is passed to `galois.GF(..., repr=field_repr)`.
References
[Finite Field](https://en.wikipedia.org/wiki/Finite_field)
Expand All @@ -899,6 +906,8 @@ class QGF(QDType):

characteristic: SymbolicInt
degree: SymbolicInt
irreducible_poly: Optional['galois.Poly'] = None
element_repr: str = 'int'

@cached_property
def order(self) -> SymbolicInt:
Expand Down Expand Up @@ -927,7 +936,13 @@ def _quint_equivalent(self) -> QUInt:
def gf_type(self):
from galois import GF

return GF(int(self.characteristic), int(self.degree), compile='python-calculate')
return GF( # type: ignore[call-overload]
int(self.characteristic),
int(self.degree),
irreducible_poly=self.irreducible_poly,
repr=self.element_repr,
compile='python-calculate',
)

def to_bits(self, x) -> List[int]:
"""Yields individual bits corresponding to binary representation of x"""
Expand Down
2 changes: 1 addition & 1 deletion qualtran/bloqs/gf_arithmetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
from qualtran.bloqs.gf_arithmetic.gf2_inverse import GF2Inverse
from qualtran.bloqs.gf_arithmetic.gf2_multiplication import (
GF2Multiplication,
MultiplyPolyByConstantMod,
GF2MultiplyByConstantMod,
)
from qualtran.bloqs.gf_arithmetic.gf2_square import GF2Square
87 changes: 52 additions & 35 deletions qualtran/bloqs/gf_arithmetic/gf2_multiplication.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "f2e9ace3",
"id": "bd16181d",
"metadata": {
"cq.autogen": "title_cell"
},
Expand All @@ -13,7 +13,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "84523966",
"id": "c6216556",
"metadata": {
"cq.autogen": "top_imports"
},
Expand All @@ -30,7 +30,7 @@
},
{
"cell_type": "markdown",
"id": "d77b58bc",
"id": "667e99e4",
"metadata": {
"cq.autogen": "GF2Multiplication.bloq_doc.md"
},
Expand Down Expand Up @@ -72,7 +72,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "40013f0c",
"id": "e7be0a06",
"metadata": {
"cq.autogen": "GF2Multiplication.bloq_doc.py"
},
Expand All @@ -83,7 +83,7 @@
},
{
"cell_type": "markdown",
"id": "c5bcc7f0",
"id": "eda29d2c",
"metadata": {
"cq.autogen": "GF2Multiplication.example_instances.md"
},
Expand All @@ -94,7 +94,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "75114297",
"id": "19a69ab5",
"metadata": {
"cq.autogen": "GF2Multiplication.gf16_multiplication"
},
Expand All @@ -106,7 +106,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7bdc2588",
"id": "a5d9f3a7",
"metadata": {
"cq.autogen": "GF2Multiplication.gf2_multiplication_symbolic"
},
Expand All @@ -120,7 +120,7 @@
},
{
"cell_type": "markdown",
"id": "9866335a",
"id": "22e6b63a",
"metadata": {
"cq.autogen": "GF2Multiplication.graphical_signature.md"
},
Expand All @@ -131,7 +131,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f09a13f8",
"id": "345d09a7",
"metadata": {
"cq.autogen": "GF2Multiplication.graphical_signature.py"
},
Expand All @@ -144,7 +144,7 @@
},
{
"cell_type": "markdown",
"id": "b26814e0",
"id": "94ab395f",
"metadata": {
"cq.autogen": "GF2Multiplication.call_graph.md"
},
Expand All @@ -155,7 +155,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7219fea5",
"id": "7701f35c",
"metadata": {
"cq.autogen": "GF2Multiplication.call_graph.py"
},
Expand All @@ -169,17 +169,17 @@
},
{
"cell_type": "markdown",
"id": "a9729be2",
"id": "410bdd3b",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.bloq_doc.md"
"cq.autogen": "GF2MultiplyByConstantMod.bloq_doc.md"
},
"source": [
"## `MultiplyPolyByConstantMod`\n",
"Multiply a polynomial by $f(x)$ modulu $m(x)$. Both $f(x)$ and $m(x)$ are constants.\n",
"## `GF2MultiplyByConstantMod`\n",
"Multiply by constant $f(x)$ modulu $m(x)$. Both $f(x)$ and $m(x)$ are constants.\n",
"\n",
"#### Parameters\n",
" - `f_x`: The polynomial to mulitply with, given either a galois.Poly or as a sequence degrees.\n",
" - `m_x`: The modulus polynomial, given either a galois.Poly or as a sequence degrees. \n",
" - `const`: The multiplication constant which is an element of the given field.\n",
" - `galois_field`: The galois field that defines the arithmetics. \n",
"\n",
"#### Registers\n",
" - `g`: The polynomial coefficients (in GF(2)). \n",
Expand All @@ -191,20 +191,20 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a25c2e49",
"id": "fcca1ecd",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.bloq_doc.py"
"cq.autogen": "GF2MultiplyByConstantMod.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.gf_arithmetic import MultiplyPolyByConstantMod"
"from qualtran.bloqs.gf_arithmetic import GF2MultiplyByConstantMod"
]
},
{
"cell_type": "markdown",
"id": "3690dcdb",
"id": "f17b748f",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.example_instances.md"
"cq.autogen": "GF2MultiplyByConstantMod.example_instances.md"
},
"source": [
"### Example Instances"
Expand All @@ -213,22 +213,39 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dffa638f",
"id": "b34f2d09",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.gf2_multiply_by_constant_modulu"
"cq.autogen": "GF2MultiplyByConstantMod.gf2_multiply_by_constant_modulu"
},
"outputs": [],
"source": [
"import galois\n",
"\n",
"mx = galois.Poly.Degrees([0, 1, 3]) # x^3 + x + 1\n",
"gf = galois.GF(2, 3, irreducible_poly=mx)\n",
"const = gf(5) # x^2 + 1\n",
"gf2_multiply_by_constant_modulu = GF2MultiplyByConstantMod(const, gf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8488436",
"metadata": {
"cq.autogen": "GF2MultiplyByConstantMod.gf2_poly_multiply_by_constant_modulu"
},
"outputs": [],
"source": [
"fx = [2, 0] # x^2 + 1\n",
"mx = [0, 1, 3] # x^3 + x + 1\n",
"gf2_multiply_by_constant_modulu = MultiplyPolyByConstantMod(fx, mx)"
"gf2_poly_multiply_by_constant_modulu = GF2MultiplyByConstantMod.from_polynomials(fx, mx)"
]
},
{
"cell_type": "markdown",
"id": "e140ad12",
"id": "0836d017",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.graphical_signature.md"
"cq.autogen": "GF2MultiplyByConstantMod.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
Expand All @@ -237,22 +254,22 @@
{
"cell_type": "code",
"execution_count": null,
"id": "501a0781",
"id": "46c85d5d",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.graphical_signature.py"
"cq.autogen": "GF2MultiplyByConstantMod.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([gf2_multiply_by_constant_modulu],\n",
" ['`gf2_multiply_by_constant_modulu`'])"
"show_bloqs([gf2_multiply_by_constant_modulu, gf2_poly_multiply_by_constant_modulu],\n",
" ['`gf2_multiply_by_constant_modulu`', '`gf2_poly_multiply_by_constant_modulu`'])"
]
},
{
"cell_type": "markdown",
"id": "292b96b9",
"id": "fea3aa3d",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.call_graph.md"
"cq.autogen": "GF2MultiplyByConstantMod.call_graph.md"
},
"source": [
"### Call Graph"
Expand All @@ -261,9 +278,9 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fad6337a",
"id": "a73ee748",
"metadata": {
"cq.autogen": "MultiplyPolyByConstantMod.call_graph.py"
"cq.autogen": "GF2MultiplyByConstantMod.call_graph.py"
},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit 7b3aef9

Please sign in to comment.