Skip to content

Extend the classical action on SelectedMajoranaFermion. - #1946

Open
maxglick wants to merge 1 commit into
quantumlib:mainfrom
maxglick:selected-majorana-fermion
Open

Extend the classical action on SelectedMajoranaFermion.#1946
maxglick wants to merge 1 commit into
quantumlib:mainfrom
maxglick:selected-majorana-fermion

Conversation

@maxglick

@maxglick maxglick commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Handle multiple selection and/or control registers (while still restricting to target gate X or Z).
Fixes #1699 .

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +144 to +146
for control_register in self.control_registers:
if vals[control_register.name] == 0:
return vals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
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

Comment on lines +165 to +167
for control_register in self.control_registers:
if vals[control_register.name] == 0:
return 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
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

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.

Write classical simulation tests for SelectedMajoranaFermion

1 participant