-
Notifications
You must be signed in to change notification settings - Fork 16
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
Allowing arbitrary initialization of the input nodes #135
base: master
Are you sure you want to change the base?
Conversation
Indeed, the attribute Statevec.Nqubit leads to problems that have to be fixed
some test fixed, some other not (related to DM and TN backend)
Add test to check simulation at the circuit level is the same as at pattern level in the statevec backend; DM to come
tests/test_statevec_backend.py
Outdated
@@ -29,8 +32,10 @@ def test_measurement_into_each_XYZ_basis(self): | |||
n = 3 | |||
k = 0 | |||
# for measurement into |-> returns [[0, 0], ..., [0, 0]] (whose norm is zero) | |||
for state in [States.plus, States.zero, States.one, States.iplus, States.iminus]: | |||
m_op = np.outer(state, state.T.conjugate()) | |||
# NOTE weird choice (MINUS is orthogonal to PLUS so zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? remove?
I obtained these benchmarks (
|
@thierry-martinez Thanks a lot, that looks great! |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #135 +/- ##
==========================================
+ Coverage 71.75% 72.17% +0.42%
==========================================
Files 30 32 +2
Lines 5353 5441 +88
==========================================
+ Hits 3841 3927 +86
- Misses 1512 1514 +2 ☔ View full report in Codecov by Sentry. |
Reported by Maxime Garnier. TeamGraphix@f377001#r142237724 The test for Pauli preprocessing in density_matrix.py should be ``` pattern._pauli_preprocessed and input_state != graphix.states.BasicStates.PLUS ``` without `not`. This bug wasn't caught by `pytest` because the density matrix backend was never tested with Pauli-preprocessed patterns. I think a natural place to add such a test is in `test_pattern.py`, by "parametrizing" `test_pauli_measurment` with `backend`. This was not enough because `test_pauli_measurment` was defined twice in the same file and the second definition was hiding the first one. Therefore, this commit: - rename the first `test_pauli_measurment` into `test_pauli_measurement_random_circuit` (fixing the typo by the way); - "parametrize" `test_pauli_measurement_random_circuit` with `backend`, testing `"statevector"` and `"densitymatrix"`; - a TODO is added for tensor network backend, since the default `graph_prep` mode (`auto`) selects the mode `parallel` despite this mode is incompatible with non-standardize patterns. I propose to leave that for another PR.
@EarlMilktea could you take a quick look at the changes? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo: duplicated comments are added only once (ex. use from __future__ import annotations
)
@@ -1,5 +1,6 @@ | |||
from typing import Optional, Union |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use from __future__ import annotations
instead.
@@ -68,7 +69,7 @@ def rand_dm(dim: int, rank: int = None, dm_dtype=True) -> DensityMatrix: | |||
return dm | |||
|
|||
|
|||
def rand_gauss_cpx_mat(dim: int, sig: float = 1 / np.sqrt(2)) -> npt.NDArray: | |||
def rand_gauss_cpx_mat(dim: int, sig: float = 1 / np.sqrt(2)) -> np.ndarray: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to use npt.NDArray
unless absolutely necessary (ex. runtime checking of return type) because we cannot specify element type.
from graphix.sim.statevec import CNOT_TENSOR, CZ_TENSOR, SWAP_TENSOR, meas_op | ||
from graphix.sim.statevec import CNOT_TENSOR, CZ_TENSOR, SWAP_TENSOR, Statevec | ||
|
||
Data = typing.Union[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing.Union
is deprecated.
graphix.states.State, | ||
"DensityMatrix", | ||
Statevec, | ||
typing.Iterable[graphix.states.State], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please import from collections.abc
instead.
return | ||
if isinstance(data, typing.Iterable): | ||
input_list = list(data) | ||
if len(input_list) != 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to compute len
.
""" | ||
|
||
@abc.abstractmethod | ||
def get_statevector(self) -> np.ndarray: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npt.NDArray
if possible.
plane: graphix.pauli.Plane | ||
angle: float | ||
|
||
def __repr__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify return type explicitly.
nqb = fx_rng.integers(2, 5) | ||
print(f"nqb is {nqb}") | ||
rand_angles = fx_rng.random(nqb) * 2 * np.pi | ||
rand_planes = fx_rng.choice(np.array([i for i in graphix.pauli.Plane]), nqb) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to cast to np.ndarray
before choice?
from graphix.states import BasicStates, PlanarState | ||
|
||
|
||
class TestStatevec(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use unittest.
# for testing with arbitrary inputs | ||
# SV and DM backend | ||
class TestPatternSim: | ||
def test_sv_sim(self, fx_rng: Generator, nqb, rand_circ): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify -> None
explicitly.
Allowing arbitrary intialization of the input nodes for statevector and density matrix backends + tests.
Several format of initialization are supported (Statevec or DensityMatrix objects, list of newly-defined State objects, numerical data).
Main change is that basic states (|0>, |+>, ...) are now in states.BasicStates and no longer in ops.py.