Skip to content

Commit 9a2d7ac

Browse files
Merge pull request #60 from ColibrITD-SAS/feat-test-exemples
Feat: test exemples
2 parents 610417b + 4fc9b83 commit 9a2d7ac

12 files changed

+157
-98
lines changed

.github/workflows/tests.yml

+29-12
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,57 @@ on:
44
push:
55
branches:
66
- dev
7+
- main
78
workflow_dispatch:
89
inputs:
910
python_v:
10-
description: 'python version'
11+
description: "python version"
1112
required: true
12-
default: '3.9'
13+
default: "3.9"
1314
type: choice
1415
options:
15-
- '3.9'
16-
- '3.10'
17-
- '3.11'
16+
- "3.9"
17+
- "3.10"
18+
- "3.11"
1819
commit_ref:
1920
description: Specific ref (branch, tag or SHA)
20-
default: ''
21+
default: ""
2122
type: string
2223
required: false
23-
24+
long:
25+
description: "Run long tests"
26+
required: false
27+
default: false
28+
type: boolean
2429

2530
jobs:
2631
test:
2732
runs-on: ubuntu-latest
2833
steps:
29-
- name: Checkout Repository
34+
- name: Checkout repository
3035
uses: actions/checkout@v4
3136
with:
3237
ref: ${{ github.event.inputs.commit_ref || github.ref }}
33-
- name: Set up Python ${{ github.event.inputs.python_v || '3.9' }}
38+
- name: Set up python ${{ github.event.inputs.python_v || '3.9' }}
3439
uses: actions/setup-python@v5
3540
with:
3641
python-version: ${{ github.event.inputs.python_v || '3.9' }}
3742
cache: "pip"
38-
- name: Install Dependencies
43+
- name: Install dependencies
3944
run: |
4045
pip install --upgrade pip
4146
pip install -r requirements-dev.txt
42-
- name: Run test
43-
run: python -m pytest
47+
- name: Install long dedendencies
48+
if: ${{ github.event.inputs.long == 'true' || github.ref_name == 'main' }}
49+
run: |
50+
pip install .
51+
sudo apt-get update
52+
sudo apt install -y poppler-utils
53+
sudo apt-get install -y texlive-latex-base texlive-pictures texlive-latex-extra
54+
- name: Run tests
55+
run: |
56+
if [ "${{ github.event.inputs.long }}" == "true" ] || [ "${{ github.ref_name }}" == "main" ]; then
57+
python -m pytest --long-local
58+
else
59+
python -m pytest
60+
fi

CONTRIBUTING.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ repository, and find the one you need to modify to achieve your goal.
9595

9696
Here are some useful scripts for when you are developing:
9797

98-
| Command | Description |
99-
| --------------------------------- | ------------------------ |
100-
| `sphinx-build -b html docs build` | Builds the documentation |
101-
| `python -m pytest` | Runs the test suite |
102-
| `python -m pytest --long` | Runs the long tests too |
98+
| Command | Description |
99+
| --------------------------------- | ------------------------- |
100+
| `sphinx-build -b html docs build` | Builds the documentation |
101+
| `python -m pytest` | Runs the test suite |
102+
| `python -m pytest --long` | Runs the long tests too |
103+
| `python -m pytest --long-local` | Runs the local long tests |
103104

104105
When making commits, make sure to follow the
105106
[conventional commit](https://www.conventionalcommits.org/en/v1.0.0/)

conftest.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import os
2-
3-
from pytest import Metafunc, Parser
1+
from pytest import Parser
42

53

64
def pytest_addoption(parser: Parser):
75
parser.addoption("--long", action="store_false", help="If set, long tests will run")
8-
9-
10-
def pytest_generate_tests(metafunc: Metafunc):
11-
print("ho")
12-
if metafunc.config.option.long:
13-
os.environ["LONG_TESTS"] = "True"
6+
parser.addoption(
7+
"--long-local",
8+
action="store_false",
9+
help="If set, only local long tests will run",
10+
)

examples/notebooks/1_Basics_of_circuit.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@
649649
"metadata": {},
650650
"outputs": [],
651651
"source": [
652-
"from mpqp.gates import symbols\n",
652+
"from sympy import symbols\n",
653653
"\n",
654654
"theta, k = symbols(\"θ k\")"
655655
]
@@ -790,7 +790,7 @@
790790
"name": "python",
791791
"nbconvert_exporter": "python",
792792
"pygments_lexer": "ipython3",
793-
"version": "3.9.12"
793+
"version": "3.9.5"
794794
}
795795
},
796796
"nbformat": 4,

examples/notebooks/4_Quantum_Fourier_Transform.ipynb

+22-27
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 1,
19+
"execution_count": 2,
2020
"id": "0e19bebf",
2121
"metadata": {},
22-
"outputs": [
23-
{
24-
"name": "stderr",
25-
"output_type": "stream",
26-
"text": [
27-
"<frozen importlib._bootstrap>:228: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject\n"
28-
]
29-
}
30-
],
22+
"outputs": [],
3123
"source": [
3224
"from mpqp.gates import *\n",
25+
"from mpqp.execution.result import Result\n",
3326
"from mpqp import QCircuit, Barrier\n",
3427
"from mpqp.execution import run, IBMDevice\n",
3528
"from math import floor\n",
@@ -46,7 +39,7 @@
4639
},
4740
{
4841
"cell_type": "code",
49-
"execution_count": 2,
42+
"execution_count": 3,
5043
"id": "ffdddefb",
5144
"metadata": {},
5245
"outputs": [],
@@ -60,12 +53,14 @@
6053
" self._build()\n",
6154
" \n",
6255
" def _build(self):\n",
63-
" \n",
64-
" self.add([H(j),[CRk(i+1,i,j) for i in range(j+1,self.nb_qubits)],Barrier()] for j in range(0,self.nb_qubits))\n",
65-
" self.add([SWAP(i,self.nb_qubits-1-i) for i in range(0,int(floor(self.nb_qubits/2)))])\n",
56+
" for j in range(self.nb_qubits):\n",
57+
" self.add(H(j))\n",
58+
" self.add([CRk(i+1, i, j) for i in range(j+1, self.nb_qubits)])\n",
59+
" self.add(Barrier())\n",
60+
" self.add([SWAP(i, self.nb_qubits-1-i) for i in range(int(floor(self.nb_qubits / 2)))])\n",
6661
" \n",
6762
" if self.inverse == True:\n",
68-
" self.inverse() "
63+
" self.inverse()"
6964
]
7065
},
7166
{
@@ -78,7 +73,7 @@
7873
},
7974
{
8075
"cell_type": "code",
81-
"execution_count": 3,
76+
"execution_count": 4,
8277
"id": "f290c657",
8378
"metadata": {
8479
"scrolled": false
@@ -88,7 +83,6 @@
8883
"name": "stdout",
8984
"output_type": "stream",
9085
"text": [
91-
"hi\n",
9286
" ┌───┐ ░ »\n",
9387
"q_0: ┤ H ├─■────────■────────■────────■─────────░────────────────────────»\n",
9488
" └───┘ │P(π/2) │ │ │ ░ ┌───┐ »\n",
@@ -133,7 +127,7 @@
133127
},
134128
{
135129
"cell_type": "code",
136-
"execution_count": 4,
130+
"execution_count": 5,
137131
"id": "ce59b0f3",
138132
"metadata": {},
139133
"outputs": [
@@ -152,8 +146,9 @@
152146
}
153147
],
154148
"source": [
155-
"result = run(qft_5, [IBMDevice.AER_SIMULATOR_STATEVECTOR])\n",
156-
"print(result.amplitudes)"
149+
"result = run(qft_5, IBMDevice.AER_SIMULATOR_STATEVECTOR)\n",
150+
"if isinstance(result, Result):\n",
151+
" print(result.amplitudes)"
157152
]
158153
},
159154
{
@@ -166,15 +161,14 @@
166161
},
167162
{
168163
"cell_type": "code",
169-
"execution_count": 5,
164+
"execution_count": 6,
170165
"id": "97f049b8",
171166
"metadata": {},
172167
"outputs": [
173168
{
174169
"name": "stdout",
175170
"output_type": "stream",
176171
"text": [
177-
"hi\n",
178172
" ┌───┐ ░ ░ \n",
179173
"q_0: ┤ H ├─■────────░───────░──X─\n",
180174
" ├───┤ │P(π/2) ░ ┌───┐ ░ │ \n",
@@ -191,20 +185,20 @@
191185
"print(qc1)\n",
192186
"\n",
193187
"result1 = run(qc1, IBMDevice.AER_SIMULATOR_STATEVECTOR) \n",
194-
"print(result1.amplitudes)"
188+
"if isinstance(result1, Result):\n",
189+
" print(result1.amplitudes)"
195190
]
196191
},
197192
{
198193
"cell_type": "code",
199-
"execution_count": 6,
194+
"execution_count": 7,
200195
"id": "67e3e10a",
201196
"metadata": {},
202197
"outputs": [
203198
{
204199
"name": "stdout",
205200
"output_type": "stream",
206201
"text": [
207-
"hi\n",
208202
" ┌────────────┐ ┌───┐┌───┐ ░ »\n",
209203
"q_0: ┤ Ry(1.9106) ├─■───────────────────■──┤ X ├┤ H ├─■────────■────────░──────»\n",
210204
" └┬──────────┬┘ │ ┌─────────┐ ┌─┴─┐└───┘└───┘ │P(π/2) │ ░ ┌───┐»\n",
@@ -233,7 +227,8 @@
233227
"print(qc2)\n",
234228
"\n",
235229
"result2 = run(qc2, IBMDevice.AER_SIMULATOR_STATEVECTOR)\n",
236-
"print(result2.amplitudes)"
230+
"if isinstance(result2, Result):\n",
231+
" print(result2.amplitudes)"
237232
]
238233
}
239234
],
@@ -253,7 +248,7 @@
253248
"name": "python",
254249
"nbconvert_exporter": "python",
255250
"pygments_lexer": "ipython3",
256-
"version": "3.9.12"
251+
"version": "3.9.5"
257252
}
258253
},
259254
"nbformat": 4,

examples/scripts/cirq_experiments.py

-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from mpqp import QCircuit
33
from mpqp.core.languages import Language
44
from mpqp.execution import run
5-
from mpqp.execution.connection.key_connection import config_ionq_key
65
from mpqp.execution.devices import GOOGLEDevice, IBMDevice
76
from mpqp.gates import H, Rx, Ry, Rz
87
from mpqp.measures import BasisMeasure
@@ -42,12 +41,3 @@
4241
processor_id = "rainbow"
4342
grid_circuit = circuit.to_other_language(Language.CIRQ, cirq_proc_id=processor_id)
4443
print(f"circuit for processor {processor_id}:\n{grid_circuit}\n")
45-
46-
# %%
47-
config_ionq_key()
48-
49-
# %%
50-
results = run(circuit, [GOOGLEDevice.IONQ_SIMULATOR])
51-
print(results)
52-
53-
results.plot()

mpqp/core/circuit.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ class QCircuit:
7474
Examples:
7575
>>> circuit = QCircuit(2)
7676
>>> circuit.pretty_print() # doctest: +NORMALIZE_WHITESPACE
77-
QCircuit : Size (Qubits,Cbits) = (2, 0), Nb instructions = 0
77+
QCircuit : Size (Qubits, Cbit) = (2, 0), Nb instruction = 0
7878
q_0:
7979
q_1:
8080
8181
>>> circuit = QCircuit(5, nb_cbits=2, label="Circuit 1")
8282
>>> circuit.add(Rx(1.23, 3))
8383
>>> circuit.pretty_print() # doctest: +NORMALIZE_WHITESPACE
84-
QCircuit Circuit 1: Size (Qubits,Cbits) = (5, 2), Nb instructions = 1
84+
QCircuit Circuit 1: Size (Qubits, Cbits) = (5, 2), Nb instruction = 1
8585
q_0: ────────────
8686
q_1: ────────────
8787
q_2: ────────────
@@ -96,7 +96,7 @@ class QCircuit:
9696
>>> circuit.add(BasisMeasure(list(range(3)), shots=2345))
9797
>>> circuit.add(Depolarizing(prob=0.50, targets=[0, 1]))
9898
>>> circuit.pretty_print() # doctest: +NORMALIZE_WHITESPACE
99-
QCircuit NoiseExample: Size (Qubits,Cbits) = (3, 3), Nb instructions = 5
99+
QCircuit NoiseExample: Size (Qubits, Cbits) = (3, 3), Nb instructions = 5
100100
Depolarizing noise: probability 0.5 on qubits [0, 1]
101101
┌───┐ ┌─┐
102102
q_0: ┤ H ├──■──┤M├───
@@ -163,7 +163,7 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]):
163163
>>> circuit.add(X(0))
164164
>>> circuit.add([CNOT(0, 1), BasisMeasure([0, 1], shots=100)])
165165
>>> circuit.pretty_print() # doctest: +NORMALIZE_WHITESPACE
166-
QCircuit : Size (Qubits,Cbits) = (2, 2), Nb instructions = 3
166+
QCircuit : Size (Qubits, Cbits) = (2, 2), Nb instructions = 3
167167
┌───┐ ┌─┐
168168
q_0: ┤ X ├──■──┤M├───
169169
└───┘┌─┴─┐└╥┘┌─┐
@@ -175,9 +175,9 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]):
175175
>>> circuit.add(Depolarizing(0.3, [0,1], dimension=2, gates=[CNOT]))
176176
>>> circuit.add([Depolarizing(0.02, [0])])
177177
>>> circuit.pretty_print() # doctest: +NORMALIZE_WHITESPACE
178-
QCircuit : Size (Qubits,Cbits) = (2, 2), Nb instructions = 3
179-
Depolarizing noise: probability 0.3 for gates [CNOT]
180-
Depolarizing noise: probability 0.02 on qubits [0]
178+
QCircuit : Size (Qubits, Cbits) = (2, 2), Nb instructions = 3
179+
Depolarizing noise: probability 0.3 for gate CNOT
180+
Depolarizing noise: probability 0.02 on qubit 0
181181
┌───┐ ┌─┐
182182
q_0: ┤ X ├──■──┤M├───
183183
└───┘┌─┴─┐└╥┘┌─┐
@@ -224,7 +224,7 @@ def add(self, components: OneOrMany[Instruction | NoiseModel]):
224224
if isinstance(components, NoiseModel):
225225
if len(components.targets) == 0:
226226
components.targets = [target for target in range(self.nb_qubits)]
227-
227+
228228
basisMs = [
229229
instr for instr in self.instructions if isinstance(instr, BasisMeasure)
230230
]
@@ -1038,7 +1038,7 @@ def pretty_print(self):
10381038
Examples:
10391039
>>> c = QCircuit([H(0), CNOT(0,1)])
10401040
>>> c.pretty_print() # doctest: +NORMALIZE_WHITESPACE
1041-
QCircuit : Size (Qubits,Cbits) = (2, 0), Nb instructions = 2
1041+
QCircuit : Size (Qubits, Cbit) = (2, 0), Nb instructions = 2
10421042
┌───┐
10431043
q_0: ┤ H ├──■──
10441044
└───┘┌─┴─┐
@@ -1047,7 +1047,7 @@ def pretty_print(self):
10471047
10481048
"""
10491049
print(
1050-
f"QCircuit {self.label or ''}: Size (Qubits,Cbits) = {self.size()},"
1050+
f"QCircuit {self.label or ''}: Size (Qubits, Cbits) = {self.size()},"
10511051
f" Nb instructions = {len(self)}"
10521052
)
10531053

@@ -1061,9 +1061,15 @@ def pretty_print(self):
10611061
targets = set(noise.targets)
10621062
noise_info = f"{type(noise).__name__} noise: probability {noise.proba}"
10631063
if targets != qubits:
1064-
noise_info += f" on qubits {noise.targets}"
1064+
noise_info += (
1065+
f" on qubit{'s' if len(noise.targets) > 1 else ''} "
1066+
f"{noise.targets[0] if len(noise.targets) == 1 else noise.targets}"
1067+
)
10651068
if noise.gates:
1066-
noise_info += f" for gates {noise.gates}"
1069+
noise_info += (
1070+
f" for gate{'s' if len(noise.gates) > 1 else ''} "
1071+
f"{noise.gates[0] if len(noise.gates) == 1 else noise.gates}"
1072+
)
10671073
print(noise_info)
10681074

10691075
print(f"{self.to_other_language(Language.QISKIT)}")

0 commit comments

Comments
 (0)