Skip to content

Commit

Permalink
Optimized implementation (GuillermoFidalgo#35)
Browse files Browse the repository at this point in the history
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/pre-commit/pre-commit-hooks: v4.5.0 → v4.6.0](pre-commit/pre-commit-hooks@v4.5.0...v4.6.0)
- [github.com/astral-sh/ruff-pre-commit: v0.3.4 → v0.3.5](astral-sh/ruff-pre-commit@v0.3.4...v0.3.5)

* Re-implementation of Qubit classes. Now represented as wavevector of system.

* Added some docstrings

* Bit of refactoring and doc; began showcase nb

* Qubit tests made compatible with new implementation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Began making Agent tets compatible with new implem

* Added numpy dependency to toml, finished basic tests for neq implem

* Testing fixed, testing in more Python versions

* Additional type hinting, documentation, fixed to gates.py, cleaning code

* Grammar fixes, added small type hinting

* Used List from typing to avoid pytest complaint for python3.8

* Higher verbosity for coverage CI

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Guillermo A. Fidalgo-Rodríguez <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 83cedc6 commit dbd4547
Show file tree
Hide file tree
Showing 7 changed files with 866 additions and 1,184 deletions.
27 changes: 17 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ name: Auto Testing
on:
pull_request:
push:
branches:
- master

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.11
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run:
python -m pip install --upgrade pip
pip install numpy coverage pytest dataclasses iniconfig packaging pluggy
pip install ./
pip install .[tests]

- name: Run Unit Tests with Coverage
run:
coverage run -m unittest discover
coverage report
run: |
coverage run -m pytest -vv
coverage report -m
coverage html
- name: Upload Coverage HTML report to Artifacts
uses: actions/upload-artifact@v2
if: always()
with:
name: coverage-report
path: htmlcov/
1,096 changes: 104 additions & 992 deletions notebooks/BB84.ipynb

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions notebooks/qCryptoShowcase.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# `qcrypto` Showcase\n",
"\n",
"This notebook presents the key features of `qcrypto`, a Python library of the simulation of simple quantum cryptography simulations. The primary classes of this library are `QstateEnt` and `QstateUnEnt`. These classes are used to represent the quantum state of a set of qubits. The former is the most general, being capable of simulating the state of a possiibility entangled set of qubits, whereas the latter can only be used to simulate a system of qubits which are unentangled."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from qcrypto.simbasics import QstateEnt, QstateUnEnt"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.05056588+0.45371015j 0.24549441+0.12567301j 0.7470044 +0.03376755j\n",
" 0.17609874+0.35406553j]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Entangled set of n qubits\n",
"n = 2\n",
"qstateent = QstateEnt(init_method=\"random\", init_nbqubits=n)\n",
"qstateent"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qstateent.measure_all(\"simult\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Unentangled set of n qubits\n",
"n = 2\n",
"# qstateunent = QstateUnEnt(init_method=\"random\", )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`qcrypto` also provides methods for applying quantum gates to the qubits, giving it the potential to be used to simulate simple quantum circuits. For instance, we can apply the Hadamard gate:\n",
"$$\n",
" H = \\frac{1}{\\sqrt{2}}\\begin{pmatrix}\n",
" 1 & 1 \\\\ 1 & -1\n",
" \\end{pmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "qcrypto"
version = "0.0.1"
version = "1.0.0"
authors = [
{name = "Roy Cruz", email = "[email protected]"},
{name = "Guillermo Fidalgo", email = "[email protected]"},
Expand All @@ -13,6 +13,15 @@ authors = [
description = "A package for simple quantum cryptography simulations."
readme = "README.md"
requires-python = ">=3.8"
dependencies = [
"numpy"
]

[project.optional-dependencies]
tests = [
"pytest",
"coverage"
]

[project.urls]
Homepage = "https://github.com/GuillermoFidalgo/QKDP"
42 changes: 42 additions & 0 deletions src/qcrypto/gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import numpy.typing as npt
from typing import Dict

Pauli: Dict[str, npt.NDArray[np.complex_]] = {
"x": np.array([[0, 1], [1, 0]], dtype=np.complex_),
"y": np.array([[0, -1j], [1j, 0]], dtype=np.complex_),
"z": np.array([[1, 0], [0, -1]], dtype=np.complex_),
}

H_gate: npt.NDArray[np.complex_] = (1 / np.sqrt(2)) * np.array(
[[1, 1], [1, -1]], dtype=np.complex_
)


def Phase_shift(phase: float) -> npt.NDArray[np.complex_]:
"""Generates a phase shift gate for a given phase.
Args:
phase (float): The phase angle in radians.
Returns:
NDArray: A 2x2 numpy array representing the phase shift gate.
"""
phase_shift_gate = np.array([1, 0], [0, np.e ** (1j * phase)])
return phase_shift_gate


def tensor_power(gate: npt.NDArray[np.complex_], N: int) -> npt.NDArray[np.complex_]:
"""Computes the tensor power of a 2x2 gate matrix.
Args:
gate (NDArray): A 2x2 numpy array representing a quantum gate.
N (int): The power to which the gate matrix is to be raised, tensor-wise.
Returns:
NDArray: A numpy array representing the N-th tensor power of the gate.
"""
result = gate
for _ in range(N - 1):
result = np.kron(result, gate)
return result
Loading

0 comments on commit dbd4547

Please sign in to comment.