Skip to content

Initial Sample Implementation - Bell's Inequality Circuit #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pytest tests
Generate a coverage report and verify that project and diff ``codecov`` are both upheld:

```bash
pytest --cov=qbraid --cov-report=term tests/
pytest --cov=qbraid_algorithms --cov-report=term tests/
```

### Build docs
Expand Down
138 changes: 138 additions & 0 deletions examples/bells_inequality.ipynb

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion qbraid_algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@

.. currentmodule:: qbraid_algorithms

Modules
-------

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

bells_inequality

"""

from . import bells_inequality
from ._version import __version__

__all__ = ["__version__"]
__all__ = ["__version__", "bells_inequality"]
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",
]
35 changes: 35 additions & 0 deletions qbraid_algorithms/bells_inequality/bells_inequality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.

"""
Bell's Inequality Experiment Implementation

Simple functions for loading and running Bell's inequality circuits.
"""

from pathlib import Path

import pyqasm
from pyqasm.modules.base import QasmModule


def load_program() -> QasmModule:
"""
Load the Bell's inequality circuit as a pyqasm module.

Returns:
pyqasm module containing the Bell's inequality circuit
"""
qasm_path = Path(__file__).parent / "bells_inequality.qasm"
return pyqasm.load(str(qasm_path))
59 changes: 59 additions & 0 deletions qbraid_algorithms/bells_inequality/bells_inequality.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Bell's Inequality Circuit based on Amazon Braket Experimental Library
OPENQASM 3.0;
include "stdgates.inc";

/*
Create bell inequality circuits
*/
// Create 3 2-qubit registries (we need 3 total circuits)
qubit[2] q0;
qubit[2] q1;
qubit[2] q2;


// Create 3 2-bit classical registries for measurment
bit[2] c0;
bit[2] c1;
bit[2] c2;

// Initialize all qubits to 0
reset q0;
reset q1;
reset q2;

/*
Prepare bell singlet states between each of the qubit pairs
*/
x q0[0];
x q0[1];
h q0[0];
cx q0[0], q0[1];

x q1[0];
x q1[1];
h q1[0];
cx q1[0], q1[1];

x q2[0];
x q2[1];
h q2[0];
cx q2[0], q2[1];

/*
Apply the rotations (angle A = 0 = no rotation)
*/

// Circuit AB
rx(pi / 3) q0[1];

// Circuit AC
rx(2 * pi / 3) q1[1];

// Circuit BC
rx(pi / 3) q2[0];
rx(2 * pi / 3) q2[1];

// Perform measurements for each of the three circuits
c0 = measure q0;
c1 = measure q1;
c2 = measure q2;
28 changes: 28 additions & 0 deletions tests/test_bells_inequality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

"""
Tests for Bell's inequality module.
"""

from pyqasm.modules.base import QasmModule

from qbraid_algorithms import bells_inequality


def test_load_program_returns_correct_type():
"""Test that load_program returns a pyqasm module object."""
circuit = bells_inequality.load_program()
# Check that it returns a valid Qasm# module module
assert isinstance(circuit, QasmModule), f"Expected QasmModule, got {type(circuit)}"