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

Register references cached by SymPy #683

Open
1 task done
thisac opened this issue Feb 24, 2022 · 0 comments
Open
1 task done

Register references cached by SymPy #683

thisac opened this issue Feb 24, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@thisac
Copy link
Contributor

thisac commented Feb 24, 2022

Before posting a bug report

  • 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

  1. A register reference object calls the par method, returning a MeasuredParameter object, inheriting from sympy.Symbol (e.g., r[0].par).
  2. A mathematical operation is applied to the MeasuredParameter object, and a sympy.Expr object is returned (e.g., 1 + r[0].par).
  3. This symbolic expression seems to get cached by SymPy.
  4. 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).
  5. 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.

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

"""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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant