Skip to content
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

doc: format fix #54

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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
88 changes: 47 additions & 41 deletions examples/notebooks/3_Expectation_value_of_observables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,29 @@
"id": "a3c9a6e8",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<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"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌───┐ ┌─────────┐ \n",
"q_0: ┤ H ├──■───┤ Ry(2.6) ├─\n",
" ───┌─┴─┐┌┴─────────┴┐\n",
"q_1: ┤ Y ├┤ X ├┤ Ry(-0.87) ├\n",
" └───┘└───┘└───────────┘\n"
" ───┌─┴─┐┌┴─────────┴┐\n",
"q_1: ─────┤ X ├┤ Ry(-0.87) ├\n",
" └───┘└───────────┘\n"
]
}
],
"source": [
"from mpqp import QCircuit\n",
"from mpqp.gates import *\n",
"circuit = QCircuit([H(0), Y(1), CNOT(0,1), Ry(2.6, 0), Ry(-0.87, 1)])\n",
"circuit = QCircuit([H(0), CNOT(0,1), Ry(2.6, 0), Ry(-0.87, 1)])\n",
"print(circuit)"
]
},
Expand Down Expand Up @@ -124,10 +131,9 @@
"output_type": "stream",
"text": [
"Result: ATOSDevice, MYQLM_PYLINALG\n",
" Expectation value: -3.317064001288396\n",
" Expectation value: -3.5935083233096687\n",
" Error/Variance: 0.0\n",
"\n",
"-3.317064001288396\n"
"-3.5935083233096687\n"
]
}
],
Expand Down Expand Up @@ -160,13 +166,12 @@
"output_type": "stream",
"text": [
"BatchResult: 2 results\n",
"Result: IBMDevice, AER_SIMULATOR\n",
" Expectation value: -3.2504999999999997\n",
" Error/Variance: 41.46215925\n",
"Result: ATOSDevice, MYQLM_PYLINALG\n",
" Expectation value: -3.2715\n",
" Error/Variance: 0.1439368300002305\n",
"\n"
" Expectation value: -3.4769999999999994\n",
" Error/Variance: 0.10704925979198084\n",
"Result: IBMDevice, AER_SIMULATOR\n",
" Expectation value: -3.6155\n",
" Error/Variance: 22.003217250000002\n"
]
}
],
Expand All @@ -187,25 +192,26 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"id": "4ac9c7e0",
"metadata": {},
"outputs": [],
"source": [
"from mpqp.measures import I, X as Pauli_X, Y as Pauli_Y, X as Pauli_Z"
"from mpqp.measures import I, X, Y, Z"
]
},
{
"cell_type": "markdown",
"id": "aed703a9",
"metadata": {},
"source": [
"> ⚠ **pauli_string import**: pauli atoms are named I, X, Y, and Z. If you have conflicts with `mpqp.gates import X, Y, Z,` , you can:\n",
"> ⚠ **pauli_string import**: pauli atoms are named `I`, `X`, `Y`, and `Z`. If\n",
"> you have conflicts with the gates of the same name, you can:\n",
"> - **Rename Import:**\n",
"> ```python \n",
"> from mpqp.measures import X as Pauli_X \n",
"> from mpqp.measures import X as obs_X \n",
"> ```\n",
"> - **Import Only Pauli String:**\n",
"> - **Import the whole module:**\n",
"> ```python\n",
"> from mpqp.measures import pauli_string \n",
"> pauli_string.X\n",
Expand Down Expand Up @@ -242,20 +248,20 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 15,
"id": "bc5ed517",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ps_1=1*I@X + -3*X@Y\n"
"ps_1=1*I@Z + -3*X@Y\n"
]
}
],
"source": [
"ps_1 = I @ Pauli_Z - 3 * Pauli_X @ Pauli_Y\n",
"ps_1 = I@Z - 3 * X@Y\n",
"print(f\"{ps_1=}\")"
]
},
Expand All @@ -276,23 +282,23 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 16,
"id": "06784ef3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ps_2 = 1*I@X + 2.555555555*Y@I + 1*X@X + -1*X@X\n",
" = 1*I@X + 2.555555555*Y@I\n",
" ~= 1*I@X + 2.6*Y@I + 1*X@X + -1*X@X\n",
" ~= 1*I@X + 2.5556*Y@I\n"
"ps_2 = 1*I@Z + 2.555555555*Y@I + 1*X@Z + -1*X@Z\n",
" = 1*I@Z + 2.555555555*Y@I\n",
" ~= 1*I@Z + 2.6*Y@I + 1*X@Z + -1*X@Z\n",
" ~= 1*I@Z + 2.5556*Y@I\n"
]
}
],
"source": [
"ps_2 = I @ Pauli_Z + 2.555555555 * Pauli_Y @ I + Pauli_X @ Pauli_Z - Pauli_X @ Pauli_Z\n",
"ps_2 = I@Z + 2.555555555*Y@I + X@Z - X@Z\n",
"print(\"ps_2 =\",repr(ps_2))\n",
"print(\" =\",repr(ps_2.simplify()))\n",
"print(\" ~=\",repr(ps_2.round(1)))\n",
Expand All @@ -311,7 +317,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"id": "b9cadb08",
"metadata": {},
"outputs": [
Expand All @@ -320,21 +326,21 @@
"output_type": "stream",
"text": [
"Addition:\n",
"(1*I@X + -3*X@Y) + (1*I@X + 2.6*Y@I) = 2*I@X + -3*X@Y + 2.6*Y@I\n",
"(1*I@Z + -3*X@Y) + (1*I@Z + 2.6*Y@I) = 2*I@Z + -3*X@Y + 2.6*Y@I\n",
"\n",
"Subtraction:\n",
"(1*I@X + -3*X@Y) - (1*I@X + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
"(1*I@Z + -3*X@Y) - (1*I@Z + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
"\n",
"Scalar product:\n",
"2 * (1*I@X + -3*X@Y) = 2*I@X + -6*X@Y\n",
"2 * (1*I@Z + -3*X@Y) = 2*I@Z + -6*X@Y\n",
"\n",
"Scalar division:\n",
"(1*I@X + 2.6*Y@I) / 3 ~= 0.3333*I@X + 0.8667*Y@I\n",
"(1*I@Z + 2.6*Y@I) / 3 ~= 0.3333*I@Z + 0.8667*Y@I\n",
"\n",
"Tensor product:\n",
"(1*I@X + -3*X@Y) @ Z = 1*I@X@X + -3*X@Y@X\n",
"(1*I@Z + -3*X@Y) @ Z = 1*I@Z@Z + -3*X@Y@Z\n",
"\n",
"(1*I@X + -3*X@Y) @ (1*I@X + 2.6*Y@I) = 1*I@X@I@X + -3*I@X@X@Y + 2.6*Y@I@I@X + -7.8*Y@I@X@Y\n"
"(1*I@Z + -3*X@Y) @ (1*I@Z + 2.6*Y@I) = 1*I@Z@I@Z + -3*I@Z@X@Y + 2.6*Y@I@I@Z + -7.8*Y@I@X@Y\n"
]
}
],
Expand All @@ -353,9 +359,9 @@
"({ps_2}) / 3 ~= {ps_2 / 3}\n",
"\n",
"Tensor product:\n",
"({ps_1}) @ Z = {ps_1 @ Pauli_Z}\n",
"({ps_1}) @ Z = {ps_1@Z}\n",
"\n",
"({ps_1}) @ ({ps_2}) = {ps_1 @ ps_2}\"\"\")"
"({ps_1}) @ ({ps_2}) = {ps_1@ps_2}\"\"\")"
]
},
{
Expand All @@ -368,7 +374,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"id": "5b0537a1",
"metadata": {},
"outputs": [],
Expand All @@ -388,7 +394,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"id": "1732e099",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -444,7 +450,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "892f0b6e",
"metadata": {},
"outputs": [],
Expand All @@ -455,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"id": "745aa999",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -506,7 +512,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
11 changes: 6 additions & 5 deletions mpqp/core/instruction/measurement/expectation_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ class Observable:
observable : can be either a Hermitian matrix representing the
observable or PauliString representing the observable.

Raises:
ValueError: If the input matrix is not Hermitian or does not have a
square shape.
NumberQubitsError: If the number of qubits in the input observable does
not match the number of target qubits.

Example:
>>> from mpqp.core.instruction.measurement.pauli_string import I, X, Y, Z
>>> matrix = np.array([[1, 0], [0, -1]])
>>> ps = 3 * I @ Z + 4 * X @ Y
>>> obs = Observable(matrix)
>>> obs2 = Observable(ps)

Raises:
ValueError: If the input matrix is not Hermitian or does not have a
square shape.
NumberQubitsError: If the number of qubits in the input observable does
not match the number of target qubits.
"""

def __init__(self, observable: Matrix | PauliString):
Expand Down
12 changes: 7 additions & 5 deletions mpqp/core/instruction/measurement/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PauliString:
- **Import Only Pauli String:** ``from mpqp.core.instruction.measurement import pauli_string``, usage: ``pauli_string.X``
"""

# TODO: reformulate ?

def __init__(self, monomials: Optional[list["PauliStringMonomial"]] = None):
self._monomials: list[PauliStringMonomial] = []

Expand Down Expand Up @@ -261,15 +263,15 @@ def from_matrix(cls, matrix: Matrix) -> PauliString:
Returns:
PauliString: Pauli string decomposition of the matrix in parameter.

Raises:
ValueError: If the input matrix is not square or its dimensions are
not a power of 2.

Example:
>>> ps = PauliString.from_matrix(np.array([[0, 1], [1, 2]]))
>>> print(ps)
1*I + 1*X + -1*Z

Raises:
ValueError: If the input matrix is not square or its dimensions are
not a power of 2.

"""
if matrix.shape[0] != matrix.shape[1]:
raise ValueError("Input matrix must be square.")
Expand Down Expand Up @@ -497,7 +499,7 @@ def __repr__(self):

def __truediv__(self, other: FixedReal) -> PauliStringMonomial:
return PauliStringMonomial(
1 / other, [self] # pyright: ignore[reportArgumentType]
1 / other, [self] # pyright: ignore[reportArgumentType]
)

def __imul__(self, other: FixedReal) -> PauliStringMonomial:
Expand Down
3 changes: 0 additions & 3 deletions mpqp/execution/connection/aws_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ def get_braket_device(device: AWSDevice, is_noisy: bool = False) -> "BraketDevic
ResultType(name='Variance', observables=['x', 'y', 'z', 'h', 'i'], minShots=10, maxShots=100000),
ResultType(name='Probability', observables=None, minShots=10, maxShots=100000)]

Raises:
AWSBraketRemoteExecutionError: If the device or the region could not be
retrieved.
"""
import boto3
from botocore.exceptions import NoRegionError
Expand Down
7 changes: 4 additions & 3 deletions mpqp/execution/connection/ibm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,16 @@ def get_backend(device: IBMDevice) -> "BackendV1":
Returns:
The requested backend.

Raises:
ValueError: If the required backend is a local simulator.
IBMRemoteExecutionError: If the device was not found.

Example:
>>> brisbane = get_backend(IBMDevice.IBM_BRISBANE)
>>> brisbane.properties().gates[0].parameters
[Nduv(datetime.datetime(2024, 1, 9, 11, 3, 18, tzinfo=tzlocal()), gate_error, , 0.00045619997922344296),
Nduv(datetime.datetime(2024, 1, 9, 15, 41, 39, tzinfo=tzlocal()), gate_length, ns, 60)]

Raises:
ValueError: If the required backend is a local simulator.
IBMRemoteExecutionError: If the device was not found.
"""
# NOTE:
# Question : when a backend is present in several IBMQ instances, which instance does it use to submit jobs
Expand Down
2 changes: 1 addition & 1 deletion mpqp/execution/connection/qlm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def config_qlm_account(username: str, password: str, global_config: bool) -> boo
outside MPQP.

Raises:
QLMRemoteExecutionError: TODO
QLMRemoteExecutionError
"""
# store the username and password in environment variables QLM_USER and QLM_PASSWD in .mpqp
prev_user = get_env_variable("QLM_USER")
Expand Down
Loading
Loading