Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,46 @@ on:
branches: [ master ]

jobs:
lint:
name: Lint (black+pyright)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.14'
architecture: 'x64'
- name: Install requirements
run: pip install .[lint,test]
- name: Lint
run: ./lint.sh
tests-ubuntu:
strategy:
matrix:
python-version: [ '3.11', '3.14' ]
name: Tests (Ubuntu)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: ${{ matrix.python-version }}
architecture: 'x64'
- name: Install requirements
run: |
pip install -r requirements.txt
pip install pytest>=5.3 pycodestyle
pip install cirq qiskit qsharp
- name: Check style
run: pycodestyle .
run: python -m pip install ".[test]"
- name: Run tests
run: pytest .

tests-windows:
name: Tests (Windows)
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: '3.14'
architecture: 'x64'
- name: Install requirements
run: |
pip install -r requirements.txt
pip install pytest>=5.3 pycodestyle
pip install cirq qiskit qsharp
run: python -m pip install ".[test]"
- name: Run tests
run: pytest .
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019-2025 Dmytro Fedoriaka
Copyright (c) 2019-2026 Dmytro Fedoriaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 11 additions & 2 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
autopep8 --in-place --aggressive --aggressive -r quantum_decomp
pycodestyle quantum_decomp
result=0

echo "Running black..."
black --check ./quantum_decomp --fast
result+=$?

echo "Running pyright..."
pyright ./quantum_decomp
result+=$?

exit $result
47 changes: 47 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools>=77.0.3"]

[project]
# https://packaging.python.org/en/latest/specifications/declaring-project-metadata/
name = "quantum_decomp"
version = "1.1.3"
description = "Tool for decomposing unitary matrix into quantum gates"
readme = "README.md"
requires-python = ">=3.11"
license = "MIT"
license-files = ["LICENSE"]
authors = [{ name = "Dmytro Fedoriaka" }]

# https://pypi.org/classifiers/
classifiers = [
"Development Status :: 5 - Production/Stable",
"Topic :: Scientific/Engineering",
]

# https://peps.python.org/pep-0508/
dependencies = [
"numpy>=2.0",
]

[project.optional-dependencies]
test = [
"cirq>=1.7.0",
"pytest>=9.1.1",
"qdk>=1.32",
"qiskit>=2.5.2",
]
lint = [
"black==25.1.0",
"pyright==1.1.414",
]

[project.urls]
"Homepage" = "https://github.com/fedimser/quantum_decomp"

[tool.setuptools.packages.find]
include = ["quantum_decomp*"]
namespaces = false

[tool.black]
line-length = 80
5 changes: 5 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pythonVersion": "3.14",
"typeCheckingMode": "standard",
"reportUnusedImport": "error",
}
9 changes: 9 additions & 0 deletions quantum_decomp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
two_level_decompose,
two_level_decompose_gray,
)

__all__ = [
"matrix_to_cirq_circuit",
"matrix_to_qiskit_circuit",
"matrix_to_gates",
"matrix_to_qsharp",
"two_level_decompose",
"two_level_decompose_gray",
]
43 changes: 16 additions & 27 deletions quantum_decomp/qsharp_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
an operation, which applies specified matrix to a register of qubits.
"""

import io
import tempfile
from contextlib import redirect_stdout

import cirq
import numpy as np
import qsharp
from numpy.typing import NDArray
from qdk import qsharp
from scipy.stats import unitary_group

import quantum_decomp as qd
from quantum_decomp.src.test_utils import CNOT, QFT_2, SWAP
from quantum_decomp.src.utils import permute_matrix


def change_int_endianness(val, n):
def change_int_endianness(val: int, n: int) -> int:
return sum(((val >> i) & 1) << (n - 1 - i) for i in range(n))


def change_matrix_endianness(matrix, n):
def change_matrix_endianness(
matrix: NDArray[np.complex128],
n: int,
) -> NDArray[np.complex128]:
assert matrix.shape == (2**n, 2**n)
perm = [change_int_endianness(i, n) for i in range(2**n)]
return permute_matrix(matrix, perm)


def dump_qsharp_unitary(op_code, qubits_count):
def dump_qsharp_unitary(
op_code: str,
qubits_count: int,
) -> NDArray[np.complex128]:
"""Returns unitary matrix which is implemented by Q# operation.

args:
Expand All @@ -36,28 +39,14 @@ def dump_qsharp_unitary(op_code, qubits_count):
return:
np.array - unitary matrix implemented by given operation.
"""
with tempfile.TemporaryDirectory() as tmp_dir:
with open(tmp_dir + "/qsharp.json", "w") as f:
f.write('{"files":["Op.qs"]}')
with open(tmp_dir + "/Op.qs", "w") as f:
f.write(op_code)
qsharp.init(project_root=tmp_dir,
target_profile=qsharp.TargetProfile.Base)
f = io.StringIO()
with redirect_stdout(f):
qsharp.eval(
f'Std.Diagnostics.DumpOperation({qubits_count}, Op.Op);')
dump_output = f.getvalue()

tokens = dump_output.replace('𝑖', 'j').replace('−', '-').split(' ')[1:]
values = np.array([np.complex128(x) for x in tokens], dtype=np.complex128)
assert len(values == (2**qubits_count)**2)
ans = values.reshape((2**qubits_count, 2**qubits_count))
return change_matrix_endianness(ans, qubits_count)
qsharp.eval(op_code)
values = np.array(qsharp.dump_operation("Op", qubits_count))
assert values.shape == (2**qubits_count, 2**qubits_count)
return change_matrix_endianness(values, qubits_count)


def check_on_matrix(matrix):
op_code = qd.matrix_to_qsharp(matrix, op_name='Op')
op_code = qd.matrix_to_qsharp(matrix, op_name="Op")
qubits_count = int(np.log2(matrix.shape[0]))
dump_matrix = dump_qsharp_unitary(op_code, qubits_count)
assert cirq.equal_up_to_global_phase(matrix, dump_matrix, atol=2e-4)
Expand Down
Loading
Loading