class_docstr: Tests for the ControlledQutritUnitary operation
class_docstr_tok: ["Tests","for","the","ControlledQutritUnitary","operation"]
docstr: Test if ControlledQutritUnitary properly applies mixed-polarity
  control values.
docstr_tok: ["Test","if","ControlledQutritUnitary","properly","applies","mixed-polarity","control","values","."]
code: def test_mixed_polarity_controls(
      self, control_wires, wires, control_values, tol
  ):
      """Test if ControlledQutritUnitary properly applies mixed-polarity
      control values."""
      target_wires = Wires(wires)
  
      dev = qml.device(
          "default.qutrit", wires=len(control_wires + target_wires)
      )
  
      # Pick a random unitary
      U = unitary_group.rvs(3 ** len(target_wires), random_state=1967)
  
      # Pick random starting state for the control and target qutrits
      starting_state = np.random.normal(
          size=3 ** (len(control_wires + target_wires))
      )
      starting_state = np.array(
          [
              x * 1j if int(x * 10) % 2 == 0 else x
              for x in starting_state
          ]
      )
      starting_state = starting_state / np.linalg.norm(starting_state)
      dev._state = starting_state.reshape(
          [3] * len(control_wires + target_wires)
      )
      dev.apply(
          [
              qml.ControlledQutritUnitary(
                  U,
                  control_wires=control_wires,
                  wires=target_wires,
                  control_values=control_values,
              ),
          ]
      )
      final_state = dev._state.flatten()
  
      dev.reset()
      dev._state = starting_state.reshape(
          [3] * len(control_wires + target_wires)
      )
  
      # Find which wires are controlled on "1" and "0", and shift the state of the device
      # accordingly to apply the controls, and then undo them after the controlled operation
      one_locations = [
          x
          for x in range(len(control_values))
          if control_values[x] == "1"
      ]
      zero_locations = [
          x
          for x in range(len(control_values))
          if control_values[x] == "0"
      ]
  
      ops = []
      for wire in one_locations:
          ops.append(qml.TShift(wires=control_wires[wire]))
  
      for wire in zero_locations:
          ops.append(qml.TShift(wires=control_wires[wire]))
          ops.append(qml.TShift(wires=control_wires[wire]))
  
      ops.append(
          qml.ControlledQutritUnitary(
              U,
              control_wires=control_wires,
              wires=target_wires,
          )
      )
  
      for wire in zero_locations:
          ops.append(qml.TShift(wires=control_wires[wire]))
  
      for wire in one_locations:
          ops.append(qml.TShift(wires=control_wires[wire]))
          ops.append(qml.TShift(wires=control_wires[wire]))
  
      dev.apply(ops)
  
      assert np.allclose(dev._state.flatten(), final_state, atol=tol)
  
imports: import pytest
  import numpy
  from scipy.stats import unitary_group
  from gate_data import TSWAP
  import pennylane
  from pennylane.wires import Wires
  from pennylane.operation import DecompositionUndefinedError
label_desc: Use image node_id 6 for calling the TestControlledQutritUnitary obj's underlying member method code with example usage: obj.test_mixed_polarity_controls(control_wires, wires, control_values, tol) without return types