Skip to content

Commit

Permalink
Add BinaryBackend and BinaryCircuit, updating simulator functionaliti…
Browse files Browse the repository at this point in the history
…es (#28)

This PR: 

* Added a backend and circuit for simulating non-linear topologies. 
* Added a linter. 


Co-authored-by: Roman Wixinger <[email protected]>
  • Loading branch information
paolodr98 and romanwixinger authored Dec 16, 2024
1 parent 8edb036 commit b29cd06
Show file tree
Hide file tree
Showing 32 changed files with 3,159 additions and 1,401 deletions.
647 changes: 647 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ authors:
given-names: "Angelo"
orcid: "https://orcid.org/0000-0001-7500-387X"
title: "Noisy gates for simulating quantum computers"
version: 2.0
version: 2.1
doi: "10.1103/PhysRevResearch.5.043210"
date-released: 2023-12-06
url: "https://link.aps.org/doi/10.1103/PhysRevResearch.5.043210"
92 changes: 83 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ This command installs the package in editable mode, allowing you to work directl
make will be immediately available without the need to reinstall the package.


## Quickstart
Execute the following code in a script or notebook. Add your IBM token to by defining it as the variable IBM_TOKEN = "your_token". Optimally, you save your token in a separate file that is not in your version control system, so you are not at risk of accidentally revealing your access token.
## Quickstart
You can find this quickstart implemented in the tutorial notebook [here](docs/tutorials/notebooks/tutorial_quantum_gates.ipynb).

Execute the following code in a script or notebook. Add your IBM token to by defining it as the variable IBM_TOKEN = "your_token". Optimally, you save your token in a separate file that is not in your version control system, so you are not at risk of accidentally revealing your access token.


```python
# Standard libraries
Expand All @@ -66,7 +69,7 @@ from qiskit.visualization import plot_histogram
# Own library
from quantum_gates.simulators import MrAndersonSimulator
from quantum_gates.gates import standard_gates
from quantum_gates.circuits import EfficientCircuit
from quantum_gates.circuits import EfficientCircuit, BinaryCircuit
from quantum_gates.utilities import DeviceParameters
from quantum_gates.utilities import setup_backend
IBM_TOKEN = "<your_token>"
Expand All @@ -89,7 +92,7 @@ config = {
"hub": "ibm-q",
"group": "open",
"project": "main",
"device_name": "ibmq_manila"
"device_name": "ibm_kyiv"
},
"run": {
"shots": 1000,
Expand Down Expand Up @@ -132,14 +135,72 @@ probs = sim.run(
psi0=np.array(run_config["psi0"]),
shots=run_config["shots"],
device_param=device_param_lookup,
nqubit=2)
nqubit=2,
level_opt = 0)

```
... and analyse the result.

```python
plot_histogram(probs, bar_labels=False, legend=['Noisy Gates simulation'])
```

If you want to use a non-linear topology you must use the BinaryCircuit and slightly modify the code.
First of all we modify the qubit layout to match the topology of the device.

```python
config = {
"run": {
"shots": 1000,
"qubits_layout": [0, 14],
"psi0": [1, 0, 0, 0]
}
}
```
Then also the command to import the parameter device has to change in order to import all the information of all the qubits up to the one with the max indices.

```python
device_param = DeviceParameters(list(np.arange(max(qubits_layout)+1)))
device_param.load_from_backend(backend)
device_param_lookup = device_param.__dict__()
```

Last, we perform the simulation ...
```python
sim = MrAndersonSimulator(gates=standard_gates, CircuitClass=BinaryCircuit)

t_circ = transpile(
circ,
backend,
scheduling_method='asap',
initial_layout=qubits_layout,
seed_transpiler=42
)

probs = sim.run(
t_qiskit_circ=t_circ,
qubits_layout=qubits_layout,
psi0=np.array(run_config["psi0"]),
shots=run_config["shots"],
device_param=device_param_lookup,
nqubit=2,
level_opt = 4)

counts_ng = {format(i, 'b').zfill(2): probs[i] for i in range(0, 4)}
```
... and analyse the result.

```python
plot_histogram(counts_ng, bar_labels=False, legend=['Noisy Gates simulation'])
plot_histogram(probs, bar_labels=False, legend=['Noisy Gates simulation'])
```

Remember that the ouput of the simulator follows the Big-Endian order, if you want to switch to Little-Endian order, which is the standard for Qiskit, you can use the command

```python
from quantum_gates.utilities import fix_counts

n_measured_qubit = 2
probs_little_endian = fix_counts(probs, n_measured_qubit)
plot_histogram(probs_little_endian, bar_labels=False, legend=['Noisy Gates simulation'])
```


Expand Down Expand Up @@ -178,14 +239,26 @@ testing with (the on set in tests/simulation/test_anderson_simulator.py) is avai
parameters are prepared in the tests/utility/device_parameters folder. In the future we might upgrade the tests and mock
these dependencies.

Afer activating your virtual environment, you can run the tests from root with

```bash
python -m "pytest"
```

Using this command instead of just typing `pytest` will make sure that you are using
the right Python version to run pytest. You can also just run a subset of the test, for example:

```bash
python -m pytest -k test_gates_noiseless_cnot_inv
```

# How to contribute
Contributions are welcomed and should apply the usual git-flow: fork this repo, create a local branch named
'feature-...'. Commit often to ensure that each commit is easy to understand. Name your commits
'[feature-...] Commit message.', such that it possible to differentiate the commits of different features in the
main line. Request a merge to the mainline often. Contribute to the test suite and verify the functionality with the unit tests when using a different Python version or dependency versions. Please remember to follow the
[PEP 8 style guide](https://peps.python.org/pep-0008/), and add comments whenever it helps. The corresponding
[authors](<#authors>) are happy to support you.
[PEP 8 style guide](https://peps.python.org/pep-0008/), and add comments whenever it helps. You can run the linter
with `pylint .`. The corresponding [authors](<#authors>) are happy to support you.


## Build
Expand Down Expand Up @@ -266,4 +339,5 @@ This project has been developed thanks to the effort of the following people:
* Michele Grossi ([email protected])
* Sandro Donadi
* Angelo Bassi
* Paolo Da Rold
* Roman Wixinger ([email protected])
3 changes: 1 addition & 2 deletions configuration/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
- Make sure to never add the .env file to version control. It is also in the .gitignore.
"""

import os

from dotenv import load_dotenv
import os

load_dotenv()

Expand Down
5 changes: 4 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
numpy>=1.22
qiskit>=0.39.2
qiskit>=1.2
qiskit-ibm-runtime>=0.28.0
qiskit-aer>=0.15.0
scipy
matplotlib
pylatexenc
opt-einsum
sphinx_rtd_theme
python-dotenv
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pulse shapes. This is handled by the :doc:`integrator <integrators>`.
Simulators
~~~~~~~~~~

The :doc:`LegacyMrAndersonSimulator <simulators>` can be used to simulate
The :doc:`MrAndersonSimulator <simulators>` can be used to simulate
a quantum circuit transpiled with Qiskit with a specific
:doc:`noisy gate set <gates>`.

Expand Down
2 changes: 1 addition & 1 deletion docs/source/simulators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ matrix-vector multiplication seen in the statevector simulation. Last,
the parallel attribute specifies whether or not the shots should be
executed in parallel with multiprocessing. In general, we recommend to
parallelize the outer loop of the simulation, and not the shots, as the
latter comes with a large overhead.
latter comes with a large overhead.


.. automodule:: quantum_gates.simulators
Expand Down
295 changes: 0 additions & 295 deletions docs/tutorials/notebooks/01try_ECR.ipynb

This file was deleted.

Loading

0 comments on commit b29cd06

Please sign in to comment.