-
Notifications
You must be signed in to change notification settings - Fork 5
[CQT-417] Circuit builder accepts multiple registers #658
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
base: develop
Are you sure you want to change the base?
[CQT-417] Circuit builder accepts multiple registers #658
Conversation
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Guy Puts <guy.puts@tno.nl> Co-authored-by: Guy Puts <38719377+GuyPuts@users.noreply.github.com> Co-authored-by: Juan Boschero <juan.boschero@tno.nl> Co-authored-by: rturrado <rturrado@gmail.com> Co-authored-by: Juan Carlos Boschero <juanboschero1998@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: rares1609 <79575613+rares1609@users.noreply.github.com> Co-authored-by: Oancea <rares.oancea@tno.nl>
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: rturrado <rturrado@gmail.com> Co-authored-by: Juan Boschero <juan.boschero@tno.nl> Co-authored-by: Guy Puts <guy.puts@tno.nl> Co-authored-by: Guy Puts <38719377+GuyPuts@users.noreply.github.com> Co-authored-by: Juan Carlos Boschero <juanboschero1998@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: rares1609 <79575613+rares1609@users.noreply.github.com> Co-authored-by: Oancea <rares.oancea@tno.nl>
rares1609
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue looks good to me. The CircuitBuilder now accepts multiple (qu)bit registers which can be inserted in-between instructions. I think there are two (minor) points that still need to be addressed before its merged:
- The comment concerning the assert statement, I am not certain whether it is redundant or done on purpose.
- An entry needs to be added to the Changelog.
tests/test_circuit_builder.py
Outdated
| q2_index = 5 | ||
| q3_index = 9 | ||
|
|
||
| assert q1[q1_index] == q1[q1_index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand the purpose of this assert statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! It should be q1[q1_index] == q1_index, otherwise it is completely trivial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests here look good! The only thought I would have to test it a bit further, is to have an example in which one defines a circuit, adds a couple of (qu)bit registers, then some instructions on said registers, and afterwards another set of registers + maybe instructions, to then see if the resulting circuit is as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added this scenario to the test_multiple_registers test.
| BitRegister, | ||
| QubitRegister, | ||
| RegisterManager, | ||
| Registry, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This more of a question for my own knowledge, but what is the difference between between a register and a registry? I see in register_manager.py that a registry is an OrderedDict, but I am not entirely sure what is the difference say, between a bit_registry and a bit_register. And in the case there is not a particular difference, wouldn't it be better maybe to stick to a single naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Register has a name, size, and virtual_zero_index; it represents a list of (qu)bits with specific defined behaviour to manipulate that list. A Registry is a dictionary of registers; it's an (ordered) record of registers that are identified by their name. The RegisterManager needs to record all the registers that are in the circuit and generate the virtual register from all these registers. E.g., a (qu)bit registry holds all (qu)bit registers that are part of the circuit, and the RegisterManager generates the virtual (qu)bit register from the (qu)bit registry of (qu)bit registers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the explanation! Understood now 👍
davidbakker123
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was half way through the review, but rares finished. So I will leave it here :)
opensquirrel/circuit_builder.py
Outdated
| if isinstance(register, QubitRegister): | ||
| self.register_manager.add_qubit_register(register) | ||
| if isinstance(register, BitRegister): | ||
| self.register_manager.add_bit_register(register) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move this to the RegisterManager, so add a method add_register to it.
| qubit_registry = self._get_registry(ast, QubitRegister, LibQasmParser._is_qubit_type) | ||
| bit_registry = self._get_registry(ast, BitRegister, LibQasmParser._is_bit_type) | ||
| return RegisterManager(qubit_registry, bit_registry) | ||
| return RegisterManager(qubit_registry, bit_registry) # type: ignore [arg-type] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use @overload to fix this typing issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@overload
@staticmethod
def _get_registry(
ast: Any,
register_cls: type[QubitRegister],
type_check: Callable[[Any], bool],
) -> QubitRegistry: ...
@overload
@staticmethod
def _get_registry(
ast: Any,
register_cls: type[BitRegister],
type_check: Callable[[Any], bool],
) -> BitRegistry: ...
@staticmethod
def _get_registry(
ast: Any,
register_cls: type[QubitRegister | BitRegister],
type_check: Callable[[Any], bool],
) -> Registry:
registry = OrderedDict()
for variable in filter(type_check, ast.variables):
registry[variable.name] = register_cls(variable.typ.size, variable.name)
return registry or OrderedDict({register_cls.default_name: register_cls(0)})
def _create_register_manager(self, ast: Any) -> RegisterManager:
qubit_registry = self._get_registry(ast, QubitRegister, LibQasmParser._is_qubit_type)
bit_registry = self._get_registry(ast, BitRegister, LibQasmParser._is_bit_type)
return RegisterManager(qubit_registry, bit_registry)
No description provided.