Skip to content

Commit

Permalink
added some test and migrated to qiskit 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Antonioli committed Feb 28, 2024
1 parent 6e961c7 commit 7261171
Show file tree
Hide file tree
Showing 18 changed files with 1,536 additions and 439 deletions.
1,557 changes: 1,227 additions & 330 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ classifiers=[


[tool.poetry.dependencies]
python = "<3.12,>=3.8"
python = "<3.12,>=3.9"
types-tqdm = "^4.66.0.5"
pytest-cov = "^4.1.0"
numpy = "^1.26.4"
tqdm = "^4.66.2"
matplotlib = "^3.8.3"
pandas = "^2.2.1"
qiskit-aer = "^0.13.3"
qiskit = "1.0.1"


[tool.poetry.group.dev.dependencies]
Expand Down
File renamed without changes.
11 changes: 10 additions & 1 deletion src/qimp/Filters/Gates.py → qimp/Filters/Gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ def make_translation(


def encode_number(quantumImage: qimp.ImageEncoding.QuantumImage.QuantumImage, shift: int) -> None:
"""Encode number in binary with x gates."""
"""
Encode number in binary with x gates.
Args:
quantumImage (qimp.ImageEncoding.QuantumImage.QuantumImage): The quantum image object.
shift (int): The number to be encoded.
Returns:
None
"""
number = format(shift, "0" + str(quantumImage.num_summing) + "b")
for index, element in enumerate(number[::-1]):
if element == "1":
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def FRQI(quantumImage: QuantumImage) -> None:
quantumImage.total_qubits = int(quantumImage.required_qubits + 1)
quantumImage.compute_angles()
quantumImage.init_circuit()
quantumImage.encoding = "FRQI"
else:
raise Exception("The image has allready been encoded")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from scipy.ndimage import zoom
from tqdm import tqdm


def test_image(side: int = 8) -> numpy.ndarray:
def generate_example_image(side: int = 8) -> numpy.ndarray:
"""
Creates a test image.
Expand Down Expand Up @@ -77,12 +76,10 @@ def __init__(self, image: numpy.ndarray, zooming_factor: int = 1) -> None:
Exception: Wrong image format.
"""

if isinstance(image, (list, pd.core.series.Series)): # Check if the input type is valid
self.image = np.array(image)
elif isinstance(image, np.ndarray):
if isinstance(image, np.ndarray):
self.image = image
else:
raise Exception("wrong type")
raise Exception("Wrong Image type")

if self.image.ndim == 1:
reshapeto = math.sqrt(len(self.image))
Expand All @@ -98,11 +95,12 @@ def __init__(self, image: numpy.ndarray, zooming_factor: int = 1) -> None:
self.zooming_factor = zooming_factor

if (
self.zooming_factor < 1.0
self.zooming_factor < 1.0 or self.zooming_factor > 1.0
): # If the zooming factor is smaller than one then we apply the zoom
self.image = zoom(self.image, self.zooming_factor)
self.image = np.abs(self.image)
self.image[self.image > 255] = 255


# compute the padding dimensions for row and cols.
# works also for rectangular images
Expand All @@ -122,7 +120,7 @@ def __init__(self, image: numpy.ndarray, zooming_factor: int = 1) -> None:
self.required_qubits = int(math.log2(np.shape(self.image)[0]) * 2)
# Compute the number of required qubits to index rows and cols

def show_classical_image(self) -> None:
def show_classical_image(self) -> None:
"""Show in a figure the original image, padded and eventually zoomed."""
plt.figure()
plt.imshow(self.image, cmap="gray")
Expand All @@ -149,27 +147,12 @@ def init_circuit(self) -> None:
self.circuit = QuantumCircuit(self.x_qubits, self.y_qubits, self.color_qubit, cr)

self.initial_qubits = self.circuit.num_qubits

def draw_circuit(self) -> None:
def draw_circuit(self) -> None: # pragma: no cover
"""Draws the circuit."""
plt.figure(1)
self.circuit.draw(output="mpl")
plt.show()

def insert_qubits(self, n: int, name: str = "") -> None:
"""Insert qubits in the image circuit.
Args:
n (int): number of qubits.
name (str): name to associate to the qubits.
Returns: None
"""
qr1 = QuantumRegister(n, name)
self.circuit.regs.insert(0, qr1)
# TODO: Modify for insertion on top

def add_qubits(self, n: int, name: str = "") -> None:
"""Append qubits.
Expand All @@ -183,15 +166,15 @@ def add_qubits(self, n: int, name: str = "") -> None:
extra = QuantumRegister(n, name)
self.circuit.add_register(extra)

def reverse(self) -> None:
def reverse(self) -> None: # pragma: no cover
"""Revert circuit.
Returns: None
"""
self.circuit = self.circuit.reverse_bits()

def retrieve_and_show(self, result: Result, numOfShots: int) -> None:
def retrieve_and_show(self, result: Result, numOfShots: int) -> None: # pragma: no cover
"""Run experiments and show result, it requires measurements.
Args:
Expand Down Expand Up @@ -246,7 +229,7 @@ def retrieve_and_show(self, result: Result, numOfShots: int) -> None:
plt.title("retrieved image")
plt.show()

def __info__(self) -> str:
def __info__(self) -> str: # pragma: no cover
"""Show the state of the saved data of the image, useful for the reader.
Returns: str
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/qimp/__init__.py → qimp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
__author__ = """Giacomo Antonioli"""
__email__ = "giacomo.antonioli@phd.unipi.it"
__version__ = "0.1.0"

9 changes: 4 additions & 5 deletions src/qimp/main.py → qimp/main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from ImageEncoding.Encodings import FRQI
from ImageEncoding.QuantumImage import QuantumImage, test_image

from ImageEncoding.QuantumImage import QuantumImage, generate_example_image
# Press the green button in the gutter to run the script.
if __name__ == "__main__":
image = QuantumImage(test_image(side=4), zooming_factor=1)
image = QuantumImage(generate_example_image(side=4), zooming_factor=1)
print(image.__info__())

image.show_classical_image()
print("Encoding")
FRQI(image)

print("Sobel")
# sobel(image)

print("drawing circ")
# image.draw_circuit()
image.circuit.measure([x for x in range(0, 5)], [x for x in range(0, 5)])
print(image.circuit)

import time

from qiskit import Aer, transpile
Expand Down
File renamed without changes.
File renamed without changes.
73 changes: 0 additions & 73 deletions src/conf.py

This file was deleted.

26 changes: 26 additions & 0 deletions tests/Filters/test_Gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import qiskit
from qiskit import QuantumRegister, QuantumCircuit
from qimp.Filters import Gates
import pytest
from qimp.ImageEncoding.QuantumImage import QuantumImage

def test_make_sum_gate():
"""Test the make_sum_gate function."""
expected_circuit = QuantumCircuit(QuantumRegister(1, "c"), QuantumRegister(1, "a"), QuantumRegister(1, "b"), name="Sum")
expected_circuit.cx(1, 2)
expected_circuit.cx(0, 2)

result = Gates.make_sum_gate()

assert result == expected_circuit

def test_make_carry_gate():
"""Test the make_carry_gate function."""
expected_circuit = QuantumCircuit(QuantumRegister(1, "c"), QuantumRegister(1, "a"), QuantumRegister(1, "b"), QuantumRegister(1, "c[i+1]"), name="Carry")
expected_circuit.ccx(1, 2, 3)
expected_circuit.cx(1, 2)
expected_circuit.ccx(0, 2, 3)

result = Gates.make_carry_gate()

assert result == expected_circuit
16 changes: 16 additions & 0 deletions tests/Filters/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Tests for `qimp` module."""
import qimp
from typing import Generator

import pytest


@pytest.fixture
def version() -> Generator[str, None, None]:
"""Sample pytest fixture."""
yield qimp.__version__


def test_version(version: str) -> None:
"""Sample pytest test function with the pytest fixture as an argument."""
assert version == "0.1.0"
Loading

0 comments on commit 7261171

Please sign in to comment.