Extend the classical action on SelectedMajoranaFermion. - #1946
Conversation
… selection and/or control registers.
There was a problem hiding this comment.
Code Review
This pull request updates the SelectedMajoranaFermion class to support multiple control and selection registers in both on_classical_vals and basis_state_phase, along with adding a test case for multiple selection registers. The review feedback correctly identifies that checking for active controls using simple equality against zero can raise a ValueError for multi-dimensional arrays and fails to handle control registers with a bitsize greater than one. The reviewer provides robust code suggestions to resolve these issues using NumPy array checks.
| for control_register in self.control_registers: | ||
| if vals[control_register.name] == 0: | ||
| return vals |
There was a problem hiding this comment.
Checking vals[control_register.name] == 0 will raise a ValueError: The truth value of an array with more than one element is ambiguous if the control register has a non-empty shape (since vals[control_register.name] will be a numpy array). Additionally, if a control register has bitsize > 1, the gate is only active when all control qubits are 1 (i.e., the value is 2**bitsize - 1). We should check if any of the control qubits are inactive by comparing the array/scalar against 2**bitsize - 1 using np.any.
| for control_register in self.control_registers: | |
| if vals[control_register.name] == 0: | |
| return vals | |
| for control_register in self.control_registers: | |
| if np.any(np.asarray(vals[control_register.name]) != (2 ** control_register.bitsize - 1)): | |
| return vals |
| for control_register in self.control_registers: | ||
| if vals[control_register.name] == 0: | ||
| return 1 |
There was a problem hiding this comment.
Checking vals[control_register.name] == 0 will raise a ValueError: The truth value of an array with more than one element is ambiguous if the control register has a non-empty shape (since vals[control_register.name] will be a numpy array). Additionally, if a control register has bitsize > 1, the gate is only active when all control qubits are 1 (i.e., the value is 2**bitsize - 1). We should check if any of the control qubits are inactive by comparing the array/scalar against 2**bitsize - 1 using np.any.
| for control_register in self.control_registers: | |
| if vals[control_register.name] == 0: | |
| return 1 | |
| for control_register in self.control_registers: | |
| if np.any(np.asarray(vals[control_register.name]) != (2 ** control_register.bitsize - 1)): | |
| return 1 |
Handle multiple selection and/or control registers (while still restricting to target gate X or Z).
Fixes #1699 .