Skip to content

Adding type hints to circuit module #271

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gadhvirushiraj
Copy link
Contributor

@gadhvirushiraj gadhvirushiraj commented Mar 12, 2025

PR Summary

This PR introduces type hints to the codebase using MonkeyType. Initial applied to a single circuit module, and if feasible, can be extended to other modules. (Issue: #262)

Encountered issues with pytest decorators while running through the test suite. This was resolved using pytest-monkeytype lib, which allows MonkeyType to work correctly with pytest. Still requires a bit post processing, but it is minimal.

QuTiP-QIP supports Python 3.9+, so we are unable to use Python 3.10+ syntax (e.g., | for Union). Also, MonkeyType is unable to generate in this format directly.

Files Type Hint Status

  • _decompose.py
  • base_renderer.py (already present)
  • circuit.py
  • circuitsimulator.py
  • color_theme.py (not needed)
  • mat_renderer.py already present)
  • texrenderer.py
  • text_renderer.py (already present)

@gadhvirushiraj gadhvirushiraj changed the title added type hints Adding type hints to circuit module Mar 12, 2025
arg_label: Optional[str] = None,
index: Optional[List[int]] = None,
classical_controls: Optional[List[int]] = None,
control_value: None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

control_value: Optional[int] = None

I think

Copy link
Member

@BoxiLi BoxiLi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. I found some edge cases in the comments.

classical_controls: Optional[List[int]] = None,
control_value: None = None,
classical_control_value: Optional[int] = None,
style: None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style was added in the rendering project right? The docstring is also missing. Would be nice to add the possible options here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just add a docstring pointing to the options we have.

Comment on lines +282 to +285
gates: Union[
Tuple[Gate, Gate, Gate, Gate],
Tuple[Gate, Gate, Gate, Gate, Gate, Gate, Gate, Gate],
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely not right lol. It should be a list or tuple of gates.

def add_circuit(self, qc, start=0, overwrite_user_gates=False):
def add_circuit(
self,
qc: "QubitCircuit",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use a string but not an imported class name?

):
state: Qobj,
cbits: Optional[List[int]] = None,
U_list: None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug, U_list is no longer used. We can remove it.

Comment on lines +316 to +322
def initialize(
self,
state: Optional[Qobj] = None,
cbits: Optional[List[int]] = None,
measure_results: Optional[
Union[List[int], Tuple[str, str], Tuple[str, str, str], Tuple[()]]
] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong again. I guess probably we should not return tuples with undetermined length in the code and should use a list instead? What do you think?

This should be list of int or str.

Comment on lines +402 to +404
measure_results: Optional[
Union[List[int], Tuple[str, str], Tuple[str, str, str], Tuple[()]]
] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Comment on lines +641 to +642
probabilities: Union[List[int], float64, int, List[float64]],
cbits: Optional[Union[List[List[int]], List[int], List[int64]]] = None,
Copy link
Member

@BoxiLi BoxiLi Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The float64 and int64 are a bit weird. I don't see the reason.

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

Successfully merging this pull request may close these issues.

2 participants