Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
b39487c
initial commit - created first qasm file
LukeAndreesen Jun 24, 2025
c18a508
bells inequality .qasm file update
LukeAndreesen Jun 25, 2025
c159a96
bells inequality qasm
LukeAndreesen Jun 25, 2025
1d444c8
bern-vaz example ('1001')
LukeAndreesen Jun 25, 2025
cf42572
load .qasm w/ pyqasm
LukeAndreesen Jun 25, 2025
6db3d21
fix
LukeAndreesen Jun 25, 2025
323d916
prelim autoqasm bern-vaz - needs sdk integration
LukeAndreesen Jun 25, 2025
b6cd0ff
pqasm loading fix
LukeAndreesen Jun 25, 2025
122f8c9
Update CONTRIBUTING.md
LukeAndreesen Jun 26, 2025
2fe4d33
simple bells inequality circuit example, cleaned up
LukeAndreesen Jun 26, 2025
955ad46
testing for bell's inequality qasm3 circuits
LukeAndreesen Jun 27, 2025
c2df27e
fix init docs & update measurement from qasm2 to qasm3
LukeAndreesen Jun 27, 2025
8bdcc6e
change load_circuit to load_program, create simple jupyter notebook e…
LukeAndreesen Jul 1, 2025
b262c65
fix QasmModoule Error
LukeAndreesen Jul 1, 2025
28444af
attempt to fix module import issue
LukeAndreesen Jul 1, 2025
6a1717f
formatting
LukeAndreesen Jul 1, 2025
1dbd026
fixing format issue ... hopefully
LukeAndreesen Jul 1, 2025
33ebef1
test fix
LukeAndreesen Jul 2, 2025
c02ce2c
first comparision implementation of QFT
MTakahashi-KWH Jul 14, 2025
2ee46e5
qft qasm only test and first pass at ampl amp
MTakahashi-KWH Jul 16, 2025
a7ed498
file structure change to prevent future merge conflicts
MTakahashi-KWH Jul 16, 2025
e5665c9
module compatibility correction
MTakahashi-KWH Jul 18, 2025
06dab65
minor edits and acommodating to regular workflow
MTakahashi-KWH Jul 22, 2025
76f64c1
generated test file, demo file and fixed module init. slow progress o…
MTakahashi-KWH Jul 22, 2025
9f27215
included init file for amplitude amplification
MTakahashi-KWH Jul 30, 2025
f282bec
added new extensible macro generator, need work on gate parameterizat…
MTakahashi-KWH Aug 1, 2025
49237ee
edits for formatting and procedural correctness
MTakahashi-KWH Aug 6, 2025
8b9420a
prep to ampl amplification, staging for HHL and fixes to phase est
MTakahashi-KWH Aug 6, 2025
f5281ac
debug and comment on qasmbuilder
MTakahashi-KWH Aug 6, 2025
6b93fb6
checkpoint for working on HHL and debugging named and inverse operations
MTakahashi-KWH Aug 13, 2025
991e143
squashed commit for file restructure, syntax, debug, and annotation e…
MTakahashi-KWH Aug 14, 2025
e4c4618
cleaning up init ifles and validation edits to amp ampl
MTakahashi-KWH Aug 15, 2025
a6678d6
fixed module namespace conflicts and improvements to amplitude amplif…
MTakahashi-KWH Aug 16, 2025
3bc9009
Merge branch 'main' of https://github.com/qBraid/qbraid-algorithms in…
MTakahashi-KWH Aug 16, 2025
e1a7859
fix dangling file edits
MTakahashi-KWH Aug 16, 2025
01d1c39
remove remnants of autoqasm from local branch
MTakahashi-KWH Aug 17, 2025
ef2f790
cleaned up hamil closures and heavily improved demo qasmbuilder
MTakahashi-KWH Aug 18, 2025
d0ee3de
eod commit, further debug on all and implementation of rodeo, both di…
MTakahashi-KWH Aug 20, 2025
f8d68f7
midway commit, fillout of block encoding.
MTakahashi-KWH Aug 20, 2025
2d02c65
eod work towards completion of prep select embedding
MTakahashi-KWH Aug 21, 2025
b2c0d2d
initial completion commit of prep select library, moving to debug and…
MTakahashi-KWH Aug 22, 2025
4217fa9
commenting and completed debug of prep block encoding
MTakahashi-KWH Aug 22, 2025
147f569
clearer directory name
MTakahashi-KWH Aug 22, 2025
900b67b
eod, near completion of toeplitz and diagonal libraries
MTakahashi-KWH Aug 22, 2025
ef89f6d
toeplitz complete, onto gqsp and loads
MTakahashi-KWH Aug 23, 2025
2826a75
gqsp initial
MTakahashi-KWH Aug 25, 2025
a67c310
cleanup of files and initial completion of trotter
MTakahashi-KWH Aug 25, 2025
344e2d0
improvements in subroutine calls and namespace fix in trotter
MTakahashi-KWH Aug 25, 2025
6dda783
added multi trotter and linear trotter
MTakahashi-KWH Aug 25, 2025
07b8c3b
building out unit tests
MTakahashi-KWH Aug 26, 2025
25a9600
Merge branch 'main' of https://github.com/qBraid/qbraid-algorithms in…
MTakahashi-KWH Aug 26, 2025
b995099
added and partially debugged test suite
MTakahashi-KWH Aug 26, 2025
da9bf10
progress towards final implementation using test driven debug
MTakahashi-KWH Aug 27, 2025
e5a25ef
fixed doctree and ran linter
MTakahashi-KWH Aug 27, 2025
b73d81e
100% test coverage, shift to load integrations and linting fixes
MTakahashi-KWH Aug 27, 2025
cddecfb
fixed imports to be named
MTakahashi-KWH Aug 28, 2025
128d01c
aligned naming to pep 8 convention+ linting improvements
MTakahashi-KWH Aug 28, 2025
70d883d
name fixes and duplicate code edited for linter
MTakahashi-KWH Aug 29, 2025
ac72fbd
semi final commit, changeover to full pull request, addition of final…
MTakahashi-KWH Aug 29, 2025
5661e42
test fix and removal of local package requirements used for extended…
MTakahashi-KWH Aug 29, 2025
48e41ee
doc build fixes
MTakahashi-KWH Aug 29, 2025
2f23bbf
updates to function for debug +buld and new tests
MTakahashi-KWH Aug 29, 2025
ae5ac55
update to HHL
MTakahashi-KWH Aug 29, 2025
75266b8
weekend updates for local build tests 1
MTakahashi-KWH Aug 31, 2025
8bacd1b
fixed lint check and final test pass
MTakahashi-KWH Sep 1, 2025
98f1a2c
minor edits, cannot fix bug in reSt with ampl amp as there is no trac…
MTakahashi-KWH Sep 1, 2025
e627c40
fix unit-tests + format check
ryanhill1 Sep 1, 2025
63592b5
fix ci and other stuff
TheGupta2012 Sep 4, 2025
f9db397
fix the string error
TheGupta2012 Sep 4, 2025
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
138 changes: 138 additions & 0 deletions examples/bells_inequality.ipynb

Large diffs are not rendered by default.

912 changes: 912 additions & 0 deletions examples/demo_hamiltonians.ipynb

Large diffs are not rendered by default.

497 changes: 497 additions & 0 deletions examples/demo_qasmbuilder.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 100
line_length = 120

[tool.pylint.'MESSAGES CONTROL']
max-line-length = 100
max-line-length = 120
disable = "W0108,W0511,W0401,R0902,R0903,R0913,E0401"

[tool.pylint.MASTER]
Expand Down
15 changes: 13 additions & 2 deletions qbraid_algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@

.. autosummary::
:toctree: ../stubs/

bernstein_vazirani
qft
iqft
qpe
qtran
hhl
evolution
embedding
amplitude_amplification
rodeo

"""

from . import bernstein_vazirani, iqft, qft, qpe
from ._version import __version__

__all__ = [
Expand All @@ -41,4 +46,10 @@
"iqft",
"bernstein_vazirani",
"qpe",
"qtran",
'evolution',
'embedding',
'amplitude_amplification',
'hhl',
'rodeo'
]
32 changes: 32 additions & 0 deletions qbraid_algorithms/amplitude_amplification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Module providing Amplitude Amplification implementation.

Functions
----------

.. autosummary::
:toctree: ../stubs/

AALibrary

"""

from .amp_ampl import AALibrary

__all__ = [
"AALibrary"
]
269 changes: 269 additions & 0 deletions qbraid_algorithms/amplitude_amplification/amp_ampl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Amplitude Amplification Library for Quantum Algorithms
This module implements amplitude amplification techniques for quantum algorithms,
including Grover's algorithm and general amplitude amplification. It provides
the `AALibrary` class, which extends `GateLibrary` to offer reusable quantum
subroutines for amplifying the probability amplitudes of desired quantum states.
Classes:
AALibrary(GateLibrary):
Implements Grover's algorithm and general amplitude amplification.
Usage:
- Use `grover` for unstructured search problems.
- Use `amp_ampl` for general amplitude amplification with arbitrary oracles and state preparation.
Notes:
- The library uses subroutine-based implementations for compact qasm code generation.
- Multi-controlled Z gates are used for phase inversion in the diffusion operator.
- The code is designed to be extensible for other amplitude amplification algorithms.
'''
from typing import List

from qbraid_algorithms.qtran import GateBuilder, GateLibrary, std_gates

# TODO: once again Physics notation was originally used convert to better naming
# pylint: disable=invalid-name
# mypy: disable_error_code="call-arg"

class AALibrary(GateLibrary):
"""
Amplitude Amplification Library implementing Grover's algorithm and general amplitude amplification.

This library provides quantum algorithms for amplitude amplification, including:
- Grover's algorithm for unstructured search
- General amplitude amplification for arbitrary oracles

Both algorithms use the principle of selective phase rotation to amplify desired
quantum state amplitudes while suppressing unwanted ones.
"""

name = "AmplitudeAmplification"

def __init__(self, *args, **kwargs):
"""Initialize the AALibrary by calling the parent GateLibrary constructor."""
super().__init__(*args, **kwargs)
self.name = "AmplAmp"

def grover(self, H, qubits: List[int], depth: int) -> None:
"""
Implement Grover's algorithm for quantum search.

Grover's algorithm provides a quadratic speedup for searching unsorted databases.
It uses amplitude amplification with a specific oracle (H) to amplify the amplitude
of target states while suppressing others.

The algorithm structure:
1. Initialize qubits in superposition with Hadamard gates
2. Repeat depth times:
- Apply oracle H (marks target states)
- Apply diffusion operator (inverts amplitudes about average)

Args:
H: Oracle/Hamiltonian that marks target states
qubits: List of qubit indices to operate on
depth: Number of Grover iterations to perform
"""
# Generate unique subroutine name based on parameters
name = f'Grover{len(qubits)}{H.name}{depth}'

# Check if subroutine already exists to avoid regeneration
if name in self.gate_ref:
qubit_list = "{" + " ,".join(str(i) for i in qubits) + "}"
self.call_subroutine(name, [self.call_space.format(qubit_list)])
# Alternative gate-based call (currently commented out):
# self.call_gate(name, qubits[-1], qubits[:-1])
return

# Create new gate builder for defining the subroutine
gate_system = GateBuilder()
std_library = gate_system.import_library(std_gates)
std_library.call_space = " {}"
oracle_library = gate_system.import_library(H)

# NOTE: Alternative gate-based implementation is commented out below.
# The current subroutine approach keeps generated code compact,
# whereas gates cannot use loops (would require Python loops instead).

# Alternative gate implementation (commented out):
# std_library.begin_gate(name, qargs)
# # Initial superposition
# [std_library.h(i) for i in qargs]
# # Grover iteration: Za -> Z0
# std_library.begin_loop(depth)
# std_library.comment("Za")
# oracle_library.apply(qargs)
# [std_library.h(i) for i in qargs]
# std_library.comment("Z0")
# [std_library.x(i) for i in qargs]
# std_library.controlled_op("z", (qargs[-1], qargs[:-1]), n=len(qubits)-1)
# [std_library.x(i) for i in qargs]
# [std_library.h(i) for i in qargs]
# std_library.end_loop()
# std_library.end_gate()

# Current subroutine-based implementation
register = "reg"
std_library.begin_subroutine(name, [f"qubit[{len(qubits)}] {register}"])

# Initialize all qubits in superposition
std_library.h(register)

# Main Grover iteration loop
std_library.begin_loop(depth)

# Apply oracle (marks target states with phase flip)
std_library.comment("Za")
oracle_library.apply([f"reg[{i}]" for i in range(len(qubits))])

# Apply diffusion operator (inverts amplitudes about average)
std_library.h(register)
std_library.comment("Z0")
std_library.x(register) # Flip all qubits
# Multi-controlled Z gate (phase flip when all qubits are |1⟩)
std_library.controlled_op("z",(f"{register}[0]", [f"{register}[{i}]" for i in range(len(qubits) - 1)]),
n=len(qubits) - 1
)
std_library.x(register) # Flip back
std_library.h(register)

std_library.end_loop()
std_library.end_subroutine()

# Build and merge the subroutine into main library
self.merge(*gate_system.build(), name)

# Call the created subroutine
qubit_list = "{" + " ,".join(str(i) for i in qubits) + "}"
self.call_subroutine(name, [self.call_space.format(qubit_list)])

def amp_ampl(self, Z, H, qubits: List[int], depth: int) -> None:
"""
Implement general amplitude amplification algorithm.

This is a generalization of Grover's algorithm that works with arbitrary
oracles Z and state preparation operators H. It amplifies amplitudes of
states marked by oracle Z after preparation by operator H.

The algorithm structure:
1. Unapply state preparation Z†
2. Initialize superposition
3. Repeat depth times:
- Apply state preparation H
- Unapply oracle Z†
- Apply diffusion operator Z0
- Apply oracle Z

Args:
Z: Oracle operator that marks target states
H: State preparation operator
qubits: List of qubit indices to operate on
depth: Number of amplitude amplification iterations

Note:
There's a bug in the original code where 'z' is used instead of 'Z'
in the name generation. This is preserved to maintain exact logic.
"""
name = f'AmplAmp{len(qubits)}{Z.name}{depth}'

# Check if subroutine already exists
if name in self.gate_ref:
qubit_list = "{" + " ,".join(str(i) for i in qubits) + "}"
self.call_subroutine(name, [self.call_space.format(qubit_list)])
# Alternative gate-based call (currently commented out):
# self.call_gate(name, qubits[-1], qubits[:-1])
return

# Create new gate builder for defining the subroutine
gate_system = GateBuilder()
std_library = gate_system.import_library(std_gates)
oracle_z = gate_system.import_library(Z)
state_prep_h = gate_system.import_library(H)

# NOTE: Alternative gate-based implementations are commented out below.
# Multiple different approaches were tried during development.

# Alternative gate implementation attempt 1 (commented out):
# std_library.begin_gate(name, qargs)
# std_library.call_space = " {} "
# # Initial superposition
# [std_library.h(i) for i in qargs]
# # Amplitude amplification iteration
# std_library.begin_loop(depth)
# std_library.comment("Za")
# state_prep_h.apply(qargs)
# [std_library.h(i) for i in qargs]
# std_library.comment("Z0")
# [std_library.x(i) for i in qargs]
# std_library.controlled_op("cz", (qargs[-1], qargs[:-1]), n=len(qubits)-2)
# [std_library.x(i) for i in qargs]
# [std_library.h(i) for i in qargs]
# std_library.end_loop()
# std_library.end_gate()

# Alternative gate implementation attempt 2 (commented out):
# for _ in range(depth):
# std_library.comment("Za")
# oracle_z.apply(qargs)
# [std_library.h(i) for i in qargs]
# std_library.comment("Z0")
# print((qargs[-1], qargs[:-1])) # Debug print
# std_library.controlled_op("cp", (qargs[-1], qargs[:-1]), n=len(qubits)-2)
# [std_library.h(i) for i in qargs]

# Current subroutine-based implementation
register = "reg"
std_library.begin_subroutine(name, [f"qubit[{len(qubits)}] {register}"])

# Initial unapplication of oracle (inverse preparation)
oracle_z.unapply([f"reg[{i}]" for i in range(len(qubits))])

# Initialize superposition
std_library.h(register)

# Main amplitude amplification loop
std_library.begin_loop(depth)

# Apply state preparation operator
std_library.comment("H")
state_prep_h.apply([f"reg[{i}]" for i in range(len(qubits))])

# Unapply oracle (Z†)
std_library.comment("Zp*")
oracle_z.unapply([f"reg[{i}]" for i in range(len(qubits))])

# Apply diffusion operator (same as Grover)
std_library.comment("Z0")
std_library.x(register)
std_library.controlled_op(
"z",
(f"{register}[0]", [f"{register}[{i}]" for i in range(len(qubits) - 1)]),
n=len(qubits) - 1
)
std_library.x(register)

# Reapply oracle (Z)
std_library.comment("Zp")
oracle_z.apply([f"reg[{i}]" for i in range(len(qubits))])

std_library.end_loop()
std_library.end_subroutine()

# Build and merge the subroutine
self.merge(*gate_system.build(), name)

# Call the created subroutine
# Alternative gate-based call (commented out):
# self.call_gate(name, qubits[-1], qubits[:-1])
qubit_list = "{" + " ,".join(str(i) for i in qubits) + "}"
self.call_subroutine(name, [self.call_space.format(qubit_list)])
32 changes: 32 additions & 0 deletions qbraid_algorithms/bells_inequality/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2025 qBraid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Module providing Bell's Inequality experiment implementation.

Functions
----------

.. autosummary::
:toctree: ../stubs/

load_program

"""

from .bells_inequality import load_program

__all__ = [
"load_program",
]
Loading