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

Keyword args are not passed to backend if same backend string is reused #41

Closed
co9olguy opened this issue Jan 11, 2019 · 5 comments
Closed
Assignees
Labels
bug Something isn't working frontend

Comments

@co9olguy
Copy link
Member

Putting this here because it is something I uncovered while debugging a user's code in the SF slack channel. I'm not sure what the intended behaviour that we want here is.

I will give example for "fock" backend, but also applies to "tf" backend (actually it's an engine issue).

If a user does, for example, eng.run("fock", cutoff_dim=N), then later does this again, but with a different value for the kwarg cutoff_dim, then the cutoff_dim used by the backend actually does not change.

This is because (currently) the engine checks on the second run whether the desired backend type is the same. If it is, it avoids reinitializing a backend. As a consequence, any kwargs are not passed on to the backend itself.

Code example:

import strawberryfields as sf

eng, q = sf.Engine(num_subsystems=1)

state = eng.run('fock', cutoff_dim=2, eval=False).ket()
print('shape:',state.shape) # >> shape: (2,)

state = eng.run('fock', cutoff_dim=3, eval=False).ket()
print('shape:',state.shape) # >> shape: (2,)
@co9olguy co9olguy added the bug Something isn't working label Jan 11, 2019
@josh146 josh146 self-assigned this Jan 12, 2019
@josh146
Copy link
Member

josh146 commented Jan 12, 2019

This is definitely confusing and should be fixed to match what the user expects.

Perhaps a simple solution would be a logic modification, so that if any passed keyword arguments (in this case, cutoff_dim) don't match the corresponding backend properties, the backend is reinitialized?

At the moment, this should only affect the Fock and TF backends with the cutoff_dim kwarg, but it would be nice if we could implement a solution that automatically checks all provided kwargs against the backend properties.

@co9olguy
Copy link
Member Author

Yeah, that's what I was thinking

@microwavestine
Copy link

microwavestine commented Apr 10, 2019

from engine.py run function

      elif isinstance(backend, str):
            # if backend is specified via a string and the engine already has that type of backend
            # loaded, then we should just use the existing backend
            # pylint: disable=protected-access
            if self.backend is not None and self.backend._short_name == backend and self.backend.circuit._trunc == kwargs.get('cutoff_dim'):
                pass
            else:
            # initialize backend

(added self.backend.circuit._trunc == kwargs.get('cutoff_dim') to the if statement)

I'm not sure about checking against backend part, I just guessed self.backend.circuit._trunc would return cutoff_dim based on the code below from floc backend code:

    def get_cutoff_dim(self):
        """Returns the Hilbert space cutoff dimension used.
        Returns:
            int: cutoff dimension
        """
        return self.circuit._trunc

I took a slightly different approach by adding more stricter condition i.e on what condition backend should not be initialised. I'm not familiar with the codebase so I won't be making a pull request, but hope this might inspire some ideas for better solutions.

@co9olguy
Copy link
Member Author

co9olguy commented Apr 10, 2019

Thanks for the suggestion @0xckylee

@smite
Copy link
Collaborator

smite commented May 28, 2019

This issue was fixed by #80

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

No branches or pull requests

4 participants