Skip to content

Commit

Permalink
BtoQ and BtoPS inverse, adjoint and dual (#497)
Browse files Browse the repository at this point in the history
### **User description**
**Context:** Currently. methods such as `inverse`, `adjoint` and `dual`
do not return CircuitComponents of the same type. This can be an issue
for things such as `BtoQ` and `BtoPS` where we want to still keep track
that a representation change is happening. The solution presented here
is to override these methods to return the appropriate type.

**Description of the Change:** Override `inverse`, `dual` and `adjoint`
on `BtoQ` and `BtoPS` such that they return the appropriate object
types. Separated out `test_circuit_components_utils.py`


___

### **PR Type**
enhancement, tests


___

### **Description**
- Enhanced `BtoQ` and `BtoPS` classes by adding `adjoint`, `dual`, and
`inverse` methods to ensure appropriate object types are returned.
- Fixed a bug in the `__eq__` method of the `Circuit` class by
correcting the return value.
- Improved type checking in the `reorder_abc` function in
`gaussian_integrals.py`.
- Added comprehensive tests for `BtoPS`, `BtoQ`, and `TraceOut` classes
to ensure functionality and correctness.



___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>b_to_ps.py</strong><dd><code>Implement adjoint, dual,
and inverse methods for BtoPS</code>&nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

mrmustard/lab_dev/circuit_components_utils/b_to_ps.py

<li>Added <code>adjoint</code>, <code>dual</code>, and
<code>inverse</code> methods to the <code>BtoPS</code> class.<br> <li>
These methods ensure the return of appropriate object types.<br>


</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-c530e576cf8965a0634d76cdd9dc2775fd218ee94278d8ea1f0bc7f9a65f28c2">+35/-0</a>&nbsp;
&nbsp; </td>

</tr>                    

<tr>
  <td>
    <details>
<summary><strong>b_to_q.py</strong><dd><code>Implement adjoint, dual,
and inverse methods for BtoQ</code>&nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

mrmustard/lab_dev/circuit_components_utils/b_to_q.py

<li>Added <code>adjoint</code>, <code>dual</code>, and
<code>inverse</code> methods to the <code>BtoQ</code> class.<br> <li>
These methods ensure the return of appropriate object types.<br>


</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-ca88d1b44cdc56cd9f74c0a100800ae36547c5d76a3db2af91c9c8bd33565d6c">+33/-1</a>&nbsp;
&nbsp; </td>

</tr>                    

<tr>
  <td>
    <details>
<summary><strong>gaussian_integrals.py</strong><dd><code>Enhance type
checking in reorder_abc function</code>&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

mrmustard/physics/gaussian_integrals.py

- Improved type checking in the `reorder_abc` function.



</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-a7b779ea109bae98fb3be0cb7ed5f6dba566d967f72666d24fbd01015f7962dc">+2/-2</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>                    
</table></td></tr><tr><td><strong>Bug fix</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>circuits.py</strong><dd><code>Fix return value bug in
Circuit equality method</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

mrmustard/lab_dev/circuits.py

- Fixed a bug in the `__eq__` method by correcting the return value.



</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-b2a78bf05544ee28bdbcc736c88ec4d7851cef12e34c9caab4ac98323a9028d7">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>                    
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>test_b_to_ps.py</strong><dd><code>Add tests for BtoPS
class methods</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

tests/test_lab_dev/test_circuit_components_utils/test_b_to_ps.py

<li>Added new tests for the <code>BtoPS</code> class.<br> <li> Tests
cover initialization, adjoint, dual, inverse, and
<br>representation.<br>


</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-dc4893f24c7b72b4ebb606e0a8c5b6fab8a19dbc9eab592b2c4018c123174088">+137/-0</a>&nbsp;
</td>

</tr>                    

<tr>
  <td>
    <details>
<summary><strong>test_b_to_q.py</strong><dd><code>Add tests for BtoQ
class methods</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

tests/test_lab_dev/test_circuit_components_utils/test_b_to_q.py

<li>Added new tests for the <code>BtoQ</code> class.<br> <li> Tests
cover adjoint, dual, inverse, and transformation correctness.<br>


</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-ef330780848db87efb5e27f655eef3e67989039096d6e9fd00f9d225227c8817">+156/-0</a>&nbsp;
</td>

</tr>                    

<tr>
  <td>
    <details>
<summary><strong>test_trace_out.py</strong><dd><code>Add tests for
TraceOut class</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></summary>
<hr>

tests/test_lab_dev/test_circuit_components_utils/test_trace_out.py

<li>Added new tests for the <code>TraceOut</code> class.<br> <li> Tests
cover initialization and state tracing.<br>


</details>


  </td>
<td><a
href="https://github.com/XanaduAI/MrMustard/pull/497/files#diff-af3a8a9f3790a0d3877b6deb46f7553d60c0c96e3da9b73cf29311d22a6d7b27">+71/-0</a>&nbsp;
&nbsp; </td>

</tr>                    
</table></td></tr></tr></tbody></table>

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information
  • Loading branch information
apchytr authored Oct 17, 2024
1 parent aeb466b commit 242c806
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 256 deletions.
35 changes: 35 additions & 0 deletions mrmustard/lab_dev/circuit_components_utils/b_to_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
The class representing an operation that changes Bargmann into phase space.
"""
# pylint: disable=protected-access

from __future__ import annotations
from typing import Sequence
Expand Down Expand Up @@ -53,3 +54,37 @@ def __init__(
)
self._add_parameter(Constant(s, "s"))
self.s = s

@property
def adjoint(self) -> BtoPS:
bras = self.wires.bra.indices
kets = self.wires.ket.indices
rep = self.representation.reorder(kets + bras).conj()

ret = BtoPS(self.modes, self.s)
ret._representation = rep
ret._wires = self.wires.adjoint
ret._name = self.name + "_adj"
return ret

@property
def dual(self) -> BtoPS:
ok = self.wires.ket.output.indices
ik = self.wires.ket.input.indices
ib = self.wires.bra.input.indices
ob = self.wires.bra.output.indices
rep = self.representation.reorder(ib + ob + ik + ok).conj()

ret = BtoPS(self.modes, self.s)
ret._representation = rep
ret._wires = self.wires.dual
ret._name = self.name + "_dual"
return ret

def inverse(self) -> BtoPS:
inv = super().inverse()
ret = BtoPS(self.modes, self.s)
ret._representation = inv.representation
ret._wires = inv.wires
ret._name = inv.name
return ret
37 changes: 36 additions & 1 deletion mrmustard/lab_dev/circuit_components_utils/b_to_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
The class representing an operation that changes Bargmann into quadrature.
"""

# pylint: disable=protected-access
from __future__ import annotations
from typing import Sequence

Expand Down Expand Up @@ -54,3 +54,38 @@ def __init__(
name="BtoQ",
)
self._add_parameter(Constant(phi, "phi"))
self.phi = phi

@property
def adjoint(self) -> BtoQ:
bras = self.wires.bra.indices
kets = self.wires.ket.indices
rep = self.representation.reorder(kets + bras).conj()

ret = BtoQ(self.modes, self.phi)
ret._representation = rep
ret._wires = self.wires.adjoint
ret._name = self.name + "_adj"
return ret

@property
def dual(self) -> BtoQ:
ok = self.wires.ket.output.indices
ik = self.wires.ket.input.indices
ib = self.wires.bra.input.indices
ob = self.wires.bra.output.indices
rep = self.representation.reorder(ib + ob + ik + ok).conj()

ret = BtoQ(self.modes, self.phi)
ret._representation = rep
ret._wires = self.wires.dual
ret._name = self.name + "_dual"
return ret

def inverse(self) -> BtoQ:
inv = super().inverse()
ret = BtoQ(self.modes, self.phi)
ret._representation = inv.representation
ret._wires = inv.wires
ret._name = inv.name
return ret
2 changes: 1 addition & 1 deletion mrmustard/lab_dev/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def deserialize(cls, data: dict) -> Circuit:

def __eq__(self, other: Circuit) -> bool:
if not isinstance(other, Circuit):
return false
return False
return self.components == other.components

def __getitem__(self, idx: int) -> CircuitComponent:
Expand Down
254 changes: 0 additions & 254 deletions tests/test_lab_dev/test_circuit_components_utils.py

This file was deleted.

Loading

0 comments on commit 242c806

Please sign in to comment.