From 1f9c7972ee730ca1dbbc41a124dc9bfd34c2294c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Kolarovszki?= Date: Mon, 5 Sep 2022 14:44:06 +0200 Subject: [PATCH 1/2] fix(sampling): Photon detection with uniform loss **Problem** The lossy boson sampling with uniform losses produced only zero-array samples. **Solution** It turned out, that the class `GeneralizedCliffordsUniformLossesSimulationStrategy` has an optional parameter `transmissivity` and Piquasso is supposed to specify that parameter. Some tests have been written testing the singular values of the interferometer after specifying loss. The documentation of `pq.Loss` was also unclear about the purpose of `transmissivity`, it has also been updated. --- piquasso/_backends/sampling/calculations.py | 5 +- piquasso/instructions/channels.py | 10 +++- tests/backends/sampling/test_channels.py | 53 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/backends/sampling/test_channels.py diff --git a/piquasso/_backends/sampling/calculations.py b/piquasso/_backends/sampling/calculations.py index 2765fd95..77345300 100644 --- a/piquasso/_backends/sampling/calculations.py +++ b/piquasso/_backends/sampling/calculations.py @@ -169,6 +169,9 @@ def _get_sampling_simulation_strategy( _, singular_values, _ = np.linalg.svd(state.interferometer) if np.all(np.isclose(singular_values, singular_values[0])): - return GeneralizedCliffordsUniformLossesSimulationStrategy(permanent_calculator) + uniform_transmission_probability = singular_values[0] ** 2 + return GeneralizedCliffordsUniformLossesSimulationStrategy( + permanent_calculator, uniform_transmission_probability + ) return LossyNetworksGeneralizedCliffordsSimulationStrategy(permanent_calculator) diff --git a/piquasso/instructions/channels.py b/piquasso/instructions/channels.py index 68590554..801bd839 100644 --- a/piquasso/instructions/channels.py +++ b/piquasso/instructions/channels.py @@ -145,11 +145,17 @@ def __init__(self, theta: float, mean_thermal_excitation: float = 0) -> None: class Loss(Gate, _mixins.ScalingMixin): - """Applies a loss channel to the state. + r"""Applies a loss channel to the state. + + The transmissivity is defined by :math:`t = \cos \theta`, where :math:`\theta` is + the beamsplitter parameter and the angle between the initial and resulting state. + Considering only one particle, :math:`1-t^2` is the probability of losing this + particle. Note: - Currently, this instruction can only be used along with + - Currently, this instruction can only be used along with :class:`~piquasso._backends.sampling.simulator.SamplingSimulator`. + - The parameter `transmissivity` is usually called `transmittance`. """ def __init__(self, transmissivity: np.ndarray) -> None: diff --git a/tests/backends/sampling/test_channels.py b/tests/backends/sampling/test_channels.py new file mode 100644 index 00000000..a68669c6 --- /dev/null +++ b/tests/backends/sampling/test_channels.py @@ -0,0 +1,53 @@ +# +# Copyright 2021-2022 Budapest Quantum Computing Group +# +# 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. + +import numpy as np + +import piquasso as pq + + +def test_Loss_uniform(): + with pq.Program() as program: + pq.Q() | pq.StateVector([1, 1, 1, 0, 0]) + + pq.Q(all) | pq.Loss(transmissivity=0.9) + + simulator = pq.SamplingSimulator(d=5) + state = simulator.execute(program, shots=1).state + + assert state.is_lossy + + singular_values = np.linalg.svd(state.interferometer)[1] + + assert np.allclose(singular_values, [0.9, 0.9, 0.9, 0.9, 0.9]) + + +def test_Loss_non_uniform(): + with pq.Program() as program: + pq.Q() | pq.StateVector([1, 1, 1, 0, 0]) + + pq.Q(0) | pq.Loss(transmissivity=0.4) + pq.Q(1) | pq.Loss(transmissivity=0.5) + + simulator = pq.SamplingSimulator(d=5) + state = simulator.execute(program, shots=1).state + + assert state.is_lossy + + singular_values = np.linalg.svd(state.interferometer)[1] + + assert len(singular_values[np.isclose(singular_values, 0.4)]) == 1 + assert len(singular_values[np.isclose(singular_values, 0.5)]) == 1 + assert len(singular_values[np.isclose(singular_values, 1.0)]) == 3 From 52f56ef001a9087da86d6554f1dbabfe788c43f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Kolarovszki?= Date: Sun, 30 Oct 2022 08:56:50 +0100 Subject: [PATCH 2/2] bump: 1.0 -> 2.0.0 --- CHANGELOG.md | 23 +++++++++++++++++++++++ piquasso/__init__.py | 2 +- setup.py | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cce9820c..2657e625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,28 @@ # Changelog + +## [2.0.0] - 2022-10-30 + +### Added + +- A simulator class called `TensorflowPureFockSimulator`, which uses Tensorflow + and is able to calculate gradients. + +### Changed + +- By enabling Tensorflow support, we dropped support for customizing the + permanent, hafnian and loop hafnian calculations through `Config`. + + +## [1.0.1] - 2022-09-05 + +### Fixed + +- `SamplingSimulator` along with `Loss` using uniform transmissivity + (transmittance) parameters and `ParticleNumberMeasurement` produced only-zero + samples instead of the expected samples. + + ## [1.0.0] - 2022-04-26 ### Added diff --git a/piquasso/__init__.py b/piquasso/__init__.py index 0bd1effc..96f01e0d 100644 --- a/piquasso/__init__.py +++ b/piquasso/__init__.py @@ -156,4 +156,4 @@ "LossyInterferometer", ] -__version__ = "1.0.0" +__version__ = "2.0.0" diff --git a/setup.py b/setup.py index 43ced7a8..a6bb5f7c 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ setup( name="piquasso", - version="1.0.0", + version="2.0.0", packages=find_packages(exclude=["tests.*", "tests", "scripts", "scripts.*"]), maintainer="Budapest Quantum Computing Group", maintainer_email="kolarovszki@inf.elte.hu",