Skip to content

Commit

Permalink
added cntrl_min = cntrl_max ^ 1 for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhill1 committed May 3, 2021
1 parent da281ab commit 0cd56a0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pennylane/devices/default_qubit.py
Expand Up @@ -354,15 +354,16 @@ def _apply_toffoli(self, state, axes, **kwargs):
array[complex]: output state
"""
cntrl_max = np.argmax(axes[:2])
cntrl_min = cntrl_max ^ 1
sl_a0 = _get_slice(0, axes[cntrl_max], self.num_wires)
sl_a1 = _get_slice(1, axes[cntrl_max], self.num_wires)
sl_b0 = _get_slice(0, axes[cntrl_max ^ 1], self.num_wires - 1)
sl_b1 = _get_slice(1, axes[cntrl_max ^ 1], self.num_wires - 1)
sl_b0 = _get_slice(0, axes[cntrl_min], self.num_wires - 1)
sl_b1 = _get_slice(1, axes[cntrl_min], self.num_wires - 1)

# If both controls are smaller than the target, shift the target axis down by two. If one
# control is greater and one control is smaller than the target, shift the target axis
# down by one. If both controls are greater than the target, leave the target axis as-is.
if axes[cntrl_max ^ 1] > axes[2]:
if axes[cntrl_min] > axes[2]:
target_axes = [axes[2]]
elif axes[cntrl_max] > axes[2]:
target_axes = [axes[2] - 1]
Expand All @@ -371,7 +372,7 @@ def _apply_toffoli(self, state, axes, **kwargs):

# state[sl_a1][sl_b1] gives us all of the amplitudes with a |11> for the two control qubits.
state_x = self._apply_x(state[sl_a1][sl_b1], axes=target_axes)
state_stacked_a1 = self._stack([state[sl_a1][sl_b0], state_x], axis=axes[cntrl_max ^ 1])
state_stacked_a1 = self._stack([state[sl_a1][sl_b0], state_x], axis=axes[cntrl_min])
return self._stack([state[sl_a0], state_stacked_a1], axis=axes[cntrl_max])

def _apply_swap(self, state, axes, **kwargs):
Expand Down

0 comments on commit 0cd56a0

Please sign in to comment.