class_docstr: Tests for the _sparse_expand_matrix function.
class_docstr_tok: ["Tests","for","the","_sparse_expand_matrix","function","."]
docstr: Test that a 3 qubit gate on non-consecutive non-ascending
  wires correctly expands to 4 qubits
docstr_tok: ["Test","that","a","3","qubit","gate","on","non-consecutive","non-ascending","wires","correctly","expands","to","4","qubits"]
code: def test_expand_three_nonconsecutive_nonascending_wires(self):
      """Test that a 3 qubit gate on non-consecutive non-ascending
      wires correctly expands to 4 qubits"""
      # test applied to wire 3, 1, 2
      res = qml.math.expand_matrix(
          csr_matrix(Toffoli), [3, 1, 2], [0, 1, 2, 3]
      )
      # change the control qubit on the Toffoli gate
      rows = [0, 4, 1, 5, 2, 6, 3, 7]
      Toffoli_perm = Toffoli[:, rows][rows]
      expected = csr_matrix(np.kron(I, Toffoli_perm))
      expected.sort_indices()
      res.sort_indices()
  
      assert isinstance(res, type(expected))
      assert all(res.data == expected.data)
      assert all(res.indices == expected.indices)
  
      # test applied to wire 3, 0, 2
      res = qml.math.expand_matrix(
          csr_matrix(Toffoli), [3, 0, 2], [0, 1, 2, 3]
      )
      # change the control qubit on the Toffoli gate
      expected = csr_matrix(
          np.kron(SWAP, II)
          @ np.kron(I, Toffoli_perm)
          @ np.kron(SWAP, II)
      )
      expected.sort_indices()
      res.sort_indices()
  
      assert isinstance(res, type(expected))
      assert all(res.data == expected.data)
      assert all(res.indices == expected.indices)
  
imports: from functools import reduce
  import numpy
  import pytest
  from gate_data import CNOT, II, SWAP, I, Toffoli
  from scipy.sparse import csr_matrix
  import pennylane
  from pennylane import numpy
label_desc: Use image node_id 11 for calling the TestExpandMatrixSparse obj's underlying member method code with example usage: obj.test_expand_three_nonconsecutive_nonascending_wires() without return types