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

Forward references in saving #90

Open
JacobHast opened this issue Dec 6, 2024 · 2 comments
Open

Forward references in saving #90

JacobHast opened this issue Dec 6, 2024 · 2 comments

Comments

@JacobHast
Copy link
Collaborator

If I have a component with an attribute using forward referencing,

e.g.

@quam_dataclass
class ComponentA(QuamComponent):
    x: int

@quam_dataclass
class ComponentB(QuamComponent):
     component_a = "ComponentA"

I get a ComponentA is not defined NameError when calling .save()

At the moment this is an issue for us, as we are using qubits and resonators which have resonator and qubit attributes respectively, and so to avoid circular imports we use forward referencing, which seems to work well, except that we can save our state.

@nulinspiratie
Copy link
Contributor

Forward referencing is indeed an issue, partly because determining the correct class from the string is problematic.
Can I suggest to first try adding if TYPE_CHECKING: and see if that resolves the issue. So for example:

resonator.py

from typing import TYPE_CHECKING
from quam.core import QuamComponent, quam_dataclass

if TYPE_CHECKING:
    from quam.examples.superconducting_qubits.qubit import Qubit


@quam_dataclass
class Resonator(QuamComponent):
    qubit: Qubit

qubit.py

from typing import TYPE_CHECKING
from quam.core import QuamComponent, quam_dataclass

if TYPE_CHECKING:
    from quam.examples.superconducting_qubits.resonator import Resonator


@quam_dataclass
class Qubit(QuamComponent):
    resonator: Resonator

Let me know if this resolves your issue

@JacobHast
Copy link
Collaborator Author

This doesn't seem to work unfortunately, I get an error name 'Resonator' is not defined from the line resonator: Resonator when importing from qubit.py in your example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants