You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched exisisting GitHub issues to make sure the issue does not already exist.
Expected behavior
It works to use feed-forwarding to use measured values as parameters in gate applications. Specifically, there seems to be an issue with older MeasuredParameter objects being cached and reused when using together with mathematical operation (such as in the example below).
ops.Rgate(1+r[0].par) |r[0]
Actual behavior
A register reference object calls the par method, returning a MeasuredParameter object, inheriting from sympy.Symbol (e.g., r[0].par).
A mathematical operation is applied to the MeasuredParameter object, and a sympy.Expr object is returned (e.g., 1 + r[0].par).
This symbolic expression seems to get cached by SymPy.
The cached expression is used instead of a newly created expression when they're identical, implicitly using the old register reference stored in the MeasuredParameter object which no longer exists in the programs registers (Program.registers).
An error is raised in the Program._test_regrefs method since the measured parameters register reference doesn't match the ones in the program.
Reproduces how often
Consistently, but only under very specific circumstances where SymPy seems to be caching symbolic parameters.
"""Needs fixtures in `tests/conftest.py`. Create a file with the following content and put it in the `tests` folder. Run it using pytest: `pytest tests/name_of_test_file.py`"""
import inspect
import blackbird as bb
from strawberryfields import ops
from strawberryfields.program import Program
def test_regref_inconsistency(setup_eng):
layout = inspect.cleandoc(
""" name test version 1.0 float array p0[1, 259] = {s} float array p1[1, 259] = {r0} float array p2[1, 259] = {bs1} float array p3[1, 259] = {loop1_phase} Sgate({s}, 0.0) | 0"""
)
bb.loads(layout)
prog = Program(2)
with prog.context as r:
r[0].val = 0.1
ops.Rgate(1 + r[0].par) | r[0]
Tracebacks
Traceback (most recent call last):
File "/Users/theodor/Repos/strawberryfields-td3/tests/weird_bug.py", line 36, in test_regref_inconsistency
ops.Rgate(1 + r[0].par) | r[0]
File "/Users/theodor/Repos/strawberryfields/strawberryfields/ops.py", line 142, in __or__
reg = pu.Program_current_context.append(self, reg)
File "/Users/theodor/Repos/strawberryfields/strawberryfields/program.py", line 427, in append
self._test_regrefs(op.measurement_deps)
File "/Users/theodor/Repos/strawberryfields/strawberryfields/program.py", line 399, in _test_regrefs
raise RegRefError("RegRef state has become inconsistent.")
strawberryfields.program_utils.RegRefError: RegRef state has become inconsistent.
Additional information
No response
The text was updated successfully, but these errors were encountered:
Before posting a bug report
Expected behavior
It works to use feed-forwarding to use measured values as parameters in gate applications. Specifically, there seems to be an issue with older
MeasuredParameter
objects being cached and reused when using together with mathematical operation (such as in the example below).Actual behavior
par
method, returning aMeasuredParameter
object, inheriting fromsympy.Symbol
(e.g.,r[0].par
).MeasuredParameter
object, and asympy.Expr
object is returned (e.g.,1 + r[0].par
).MeasuredParameter
object which no longer exists in the programs registers (Program.registers
).Program._test_regrefs
method since the measured parameters register reference doesn't match the ones in the program.Reproduces how often
Consistently, but only under very specific circumstances where SymPy seems to be caching symbolic parameters.
System information
Strawberry Fields: a Python library for continuous-variable quantum circuits. Copyright 2018-2020 Xanadu Quantum Technologies Inc. Python version: 3.9.6 Platform info: macOS-12.1-arm64-i386-64bit Installation path: /Users/theodor/Repos/strawberryfields-td3/strawberryfields Strawberry Fields version: 0.22.0-dev Numpy version: 1.21.3 Scipy version: 1.7.0 SymPy version: 1.8 NetworkX version: 2.6.1 The Walrus version: 0.18.0 Blackbird version: 0.5.0-dev XCC version: 0.2.0-dev TensorFlow version: 2.4.1
Source code
Tracebacks
Additional information
No response
The text was updated successfully, but these errors were encountered: