Metadata-Version: 2.4
Name: QGBx
Version: 0.1.1
Summary: An Open-Source Extensible Package for Simulating Various Statistical Distributions through a Multi-Dimensional Variational Quantum Galton Board!
Author-email: Ashraf Boussahi <a.boussahi@esi-sba.dz>, Abir Chekroun <a.chekroun@esi-sba.dz>
License: 
                                         Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright 2025 Achraf Boussahi & Abir Chekroun
        
           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.
Project-URL: Homepage, https://github.com/AshrafBoussahi/QGBx
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: matplotlib>=3.8.0
Requires-Dist: numpy>=2.3.2
Requires-Dist: PennyLane>=0.41.0
Requires-Dist: qiskit>=1.3.2
Requires-Dist: qiskit_ibm_runtime>=0.26.0
Requires-Dist: qiskit_ibm_transpiler>=0.13.1
Requires-Dist: scipy>=1.11.4
Requires-Dist: qiskit_ibm_ai_local_transpiler
Requires-Dist: qiskit[visualization]
Requires-Dist: qiskit_aer
Dynamic: license-file

# QGBx
<img width="1600" height="300" alt="QGBXhEADER" src="https://github.com/user-attachments/assets/90c3af9a-2f7b-4386-a4de-aeb187c13ebf" />

---
*"Nature isn't classical, dammit, and if you want to make a simulation of nature, you'd better make it quantum mechanical"*


QGBx is an open-source, extensible package designed as a submission for the <a href="https://www.thewiser.org/quantum-walks-monte-carlo" target="_blank">Project 1:</a> "Quantum Walks and Monte Carlo" hosted by the Wiser-Womanium Quantum Summer Program. This package serves as a sandbox for generating several statistical distributions using different Variational Multi-Dimensional Quantum Galton Board architectures, mainly derived from the <a href="https://arxiv.org/abs/2202.01735"> Universal Statistical Distribution</a> paper by Mark Carney & Ben Varcoe., along with the authors’ efforts to optimize its structure and results for various uses under different noiseless and noisy quantum simulations and even an end-point to run the generated circuits on a real QPU.

## Installation

You can install QGBx using

```bash
pip install --upgrade QGBx
```

## Usage

QGBx was designed to be extensible in the future, where other Distributions, Devices, Visualization, and Analysis methods can be supported through the same architecture for educational purposes of studying the behavior of the Galton board under both classical and quantum physics, where it was proven that it may help to achieve universal statistical simulators and probability distributions' encoders.

The diagram below shows the relationship between the different objects and the steps of creating, simulating, and retrieving results using QGBx:

<p align="center">
  <img src="https://github.com/user-attachments/assets/188ead0c-ddd5-4356-9b2f-eaad6d1d27da" 
       alt="Qgbx Diagram Usage drawio" 
       width="700" />
</p>

### 1. Creating a Device: 

QGBx supports two kinds of `Device`: **Simulators** and **Real Devices**.

- **Simulators**  
  - Noiseless all-to-all simulators: `PennylanDefaultQubit` and `QiskitAerSimulator`.  
  - Noisy simulator: `QiskitFakeTorino` — simulates the noise effects of the Heron IBM Torino device.  

- **Real Devices**  
  - `IBM_Torino`: Runs your generated circuit on the real QPU.  
    > Requires a personal IBM token and a CRN code for a created instance.

You can create a device object using QGBx as follows:

```python
# Import the desired device class
from QGBx.devices import QiskitAerSimulator, PennylanDefaultQubit, QiskitFakeTorino, IBM_Torino

# Create a PennylanDefaultQubit device (noiseless simulator)
dev = PennylanDefaultQubit(shots=1000)

# Create a QiskitAerSimulator device (noiseless simulator)
dev = QiskitAerSimulator(shots=1000)

# Create a QiskitFakeTorino device (noisy simulator replicating IBM Torino)
dev = QiskitFakeTorino(ai_optimized=False, optimization_level=0, ai_optimization_level=0, shots=1000)

# Create an IBM_Torino device (real QPU) - requires IBM token and CRN code
dev = IBM_Torino(token="YOUR_IBM_TOKEN", instance_CRN="YOUR_CRN_CODE", optimization_level=1, shots=1000)
```
> **Note:**  
> - `shots`: The number of circuit executions (measurements).  
> - `optimization_level`: Qiskit's built-in preset pass manager circuit optimization level (0–3). Higher values can reduce circuit depth but may change gate structure.
> - `ai_optimized`: Boolean (`True`/`False`). Enables or disables the generate_ai_pass_manager.  
> - `ai_optimization_level`: QGBx's AI-based optimization level (0–3). Controls how much the ai_pass manager try to optimize circuit depth and transpilation.  

### 2. Creating a Distribution

QGBx supports three built-in distributions:  

- `Gaussian` — Which is essentially a binomial distribution that converges toward a Gaussian distribution under the Laplace–de Moivre theorem when the number of layers of the Galton board becomes large.
  
> **Note:**  
> -`Distribution` object should take a device as the first parameter :)

```python
from QGBx.distributions import Gaussian

# p is a parameter of the binomial distribution that controls its bias (0.5 is centered)
dist = Gaussian(dev, p=0.4) 
```

- `Exponential` — A distribution where the probabilities follow an exponential decay pattern, defined by a rate parameter (λ).

```python
from QGBx.distributions import Exponential

# rate is λ (lambda) of the exponential function
dist = Exponential(dev, rate=0.5)
```
 

- `Hadamard_QW` — A quantum walk distribution generated by Hadamard operations, with different types that define the result of the walk (related to the input state — see the literature for more).

```python
from QGBx.distributions import HadamardQW

# type defines the result of the Hadamard walk (related to the input state)
# type options: ["Symmetric", "Asymmetric_Right", "Asymmetric_Left"]
dist = HadamardQW(dev, type="Symmetric")
```  

The distribution object will then be filled with the results of the simulation and its ideal (theoretical) counterpart in the next step.  

The package also gives the user the ability to create a custom Galton board by manipulating the probabilities of pegs in two ways:  
- Per peg — each individual peg of the Galton board gets its own probability (of going left or right) using `PegControlled`.  
- Per layer — all pegs of the same layer get the same probability using `LayerControlled` *(not yet tested)*.  

The user can give specific probabilities for each peg (or layer) or can set a target probability they want, and the package will try to numerically (variationally) tune the pegs (or layers) for that specific target (if possible) using the *least squares* method.  

> The user can also directly define the angles of the RX gates. See the structure of the circuits to better understand.

For this distribution the target probabilities "target=" (and effectively the angles "angles=") should be a list encoded as shown in the picture below:

<p align="center">
  <img src="https://github.com/user-attachments/assets/1efa4e38-c473-4fc9-976e-bb057f23ac22" 
       alt="PegsNaming drawio"
       width="500" />
</p>

The code for it is like the following:

```python
from QGBx.distributions import PegControlled

# probs (and angles) should be a list encoded as shown in the picture above
dist = PegControlled(device, angles=None, probs=None, optimizer="least_squares", target=None)
```

There are also the `GaussianOp` and `ExponentialOp`, which follow the same logic of the Galton board but use another circuit designed by the authors for the purpose of the Project. This alternative design overcomes many shortcomings in circuit depth and efficiency compared to the main one proposed in the *Universal Statistical Simulator* method.  

They can be instantiated as follows:  

```python
from QGBx.distributions import ExponentialOp, GaussianOp

dist = ExponentialOp(dev, rate=0.5) # rate is λ (lambda) of the exponential function

dist = GaussianOp(dev, p=0.4) # p is a parameter of the binomial distribution that controls its bias (0.5 is centered)
```

### 3. Creating a Generator

In this package, the `Generator` is the orchestrator that creates the circuit for a specific Quantum Galton Board architecture to achieve a given `Distribution` using a chosen `Device`.  

It can be instantiated as follows:  

```python
import QGBx

gen = QGBx.Generator(dev, dist)
```

After that, it provides the user with the following methods:

---
#### `galton_board(n_layers)`

Build the Galton Board circuit.

**Parameters**:  
- `n_layers` *(int)* – Number of layers in the Galton Board.

**Returns**:  
- *Quantum circuit object* – The constructed Galton Board circuit and it will automatically assign the circuit to the device, and will be ready for the running phase



#### `run()`
Execute the Galton Board circuit on the configured device.  

**Returns:**  
- `dict` – One-hot probability distribution from execution and will assign the measured probabilities to the Distribution object, and calculate the ideal probabilities and assign them to the same.



#### `job_results(jobID)`
Retrieve results for a given IBM job ID after using the job on the real device is completed (it may take time if the QPU is busy).  

**Parameters:**  
- `jobID` (`str`): IBM Quantum job ID.  

**Returns:**  
- `dict` – One-hot probability distribution.



#### `export_circuit_as_png(fold=-1, filename="circuit.png", style="black_white")`
Export the circuit diagram as a PNG image.  

**Parameters:**  
- `fold` (`int`): Folding parameter for circuit visualization. set to 25 if you want the circuit to be splitted (only with qiskit circuits)  
- `filename` (`str`): Output filename (and directory) 
- `style` (`str`): Diagram style. Only the supported



#### `draw_circuit(fold=-1)`
Draw the circuit using the device's visualization method.  

**Parameters:**  
- `fold` (`int`): Folding parameter for circuit visualization. set to 25 if you want the circuit to be splitted (only with qiskit circuits)



#### `export_qasm(version="2", filename="exported_circuit.txt")`
Export the circuit as a QASM file (only with qiskit circuits)  

**Parameters:**  
- `version` (`str`): QASM version. "2" or "3" 
- `filename` (`str`): Output filename (and directory)

---

### 4. Generating the n_layers galton board:

This step can be achieved through the genertaor `.galton_board(n_layers)` method as follow:

```python
n_layers = 6
gen.galton_board(n_layers) #bigger n_layers (more than 6 in some machines) will become computaionally hard if not used the Optimized circuit
```

> - it will automatically assign the circuit to the device, and will be ready for the running

### 6. Running the circuit:
This step can be achieved through the genertaor `.run()` method as follow:

```python
gen.run()
```

> - This will assign the measured probabilities to the Distribution object, and calculate the ideal probabilities and assign them to the same.

### 5. Analyze and Visualize the Results

After retrieving the measured and the ideal theoretical distributions, we can visualize the results through plotting, and analyze the circuit's performance by comparing the measured and the ideal distributions using different metrics.

#### Visualizing:
QGBx includes a `Visualizer` object that takes a `Distribution` object when instantiated, and gives the user the ability to:


---
#### `plot(interval_length=None, ideal=False)`

Plot the probability distribution (measured or ideal if "ideal" = True)

**Parameters**:  
- interval_length (int, optional): Number of bins to display.
- ideal (bool): Whether to plot the ideal distribution instead of measured.

This can be achieved through:
```python
QGBx.Visualiser(dist).plot()
```

this will result in the following:
<p align="center">
  <img alt="ExponentialGlaton" src="https://github.com/user-attachments/assets/11c8b365-9a34-4e0d-a42c-dab31fb5bb91"
       width="500" />
</p>


---

#### `plot_with_ideal(interval_length=None)`

Plot the measured probability distribution with the ideal distribution overlaid as points connected with lignes

**Parameters**:  
- interval_length (int, optional): Number of bins to display.
- ideal (bool): Whether to plot the ideal distribution instead of measured.

This can be achieved through:
```python
QGBx.Visualiser(dist).plot_with_ideal()
```

this will result in the following:
<p align="center">
  <img alt="Expo" src="https://github.com/user-attachments/assets/2bb8d6e4-65d2-40e0-a2e6-c4b901c78dad"
       width="500" />
</p>

---

#### Analyzing:
QGBx includes the `Analyzer` object that takes a `Distribution` object when instantiated that give the ability to benchmark the measured probability distribution against the ideal theoretical one to assess the accuracy of the circuit and the method through various metrics, and can print a summary of the results.  
This object also gives the user the ability to define a tolerance for each test in order to generate a report indicating whether the circuit achieved satisfactory results.

The `Analyzer` have the following methods:

| Method | Description |
|--------|-------------|
| `total_variation_distance()` | Calculate the Total Variation Distance (TVD) between the ideal (theoritical) and measured distributions. TVD measures the largest possible probability difference between two distributions. Values closer to 0 indicate high similarity. |
| `jensen_shannon_divergence()` | Calculate the Jensen–Shannon Divergence (JSD) between the ideal (theoritical) and measured distributions. JSD is symmetric and bounded between 0 and 1. A value close to 0 means the distributions are identical. |
| `chi_squared_test()` | Calculate the Chi-Squared statistic. Smaller values indicate that the measured results are close to the ideal distribution. |
| `hellinger_distance()` | Calculate the Hellinger Distance between the ideal and quantum distributions. Values range between 0 (identical) and 1 (maximally different). Values < 0.1 are generally acceptable. |
| `entropy_difference()` | Calculate the absolute difference in **Shannon entropy** between the ideal and measured distributions. it measures randomness and uncertaintywhere smaller differences mean the distributions have similar uncertainty levels. |
| **`analyze(thresholds=None, show_passed=True)`** | Run all analysis metrics and display results in a matplotlib table. Updates pass/fail status using thresholds and displays a formatted table of results. |
| **`get_analyze_results()`** | Return the results of the last analysis as a dictionary. If `analyze()` hasn't been run, it is executed automatically without displaying the table. |


This object can be used in the defined pipeline as follow:

```python
Analyzer = QGBx.Analyzer(dist)
Analyzer.analyze(show_passed=False)
```

This will results in the following output summary:
<p align="center">
  <img alt="Intrprt" src="https://github.com/user-attachments/assets/762b4585-a804-4212-af9a-aece8a1b52ec"
       width="800" />
</p>

The user can also define thresholds and compare the results to the:

```python
Analyzer = QGBx.Analyzer(dist)
Analyzer.analyze(thresholds = [0.02,0.05,0.1,0.1,0.05], show_passed=True)
```

This will results in the following output summary:
<p align="center">
  <img alt="Intrprt" src="https://github.com/user-attachments/assets/32c48c5c-2214-4db1-9e5d-102998db9b27"
       width="800" />
</p>













