class_docstr: Pattern matching circuit optimization tests.
class_docstr_tok: ["Pattern","matching","circuit","optimization","tests","."]
docstr: Test pattern matching algorithm for circuit optimization with a template having Toffoli gates.
docstr_tok: ["Test","pattern","matching","algorithm","for","circuit","optimization","with","a","template","having","Toffoli","gates","."]
code: def test_template_with_toffoli(self):
      """Test pattern matching algorithm for circuit optimization with a template having Toffoli gates."""
  
      def circuit():
          qml.Toffoli(wires=[0, 2, 4])
          qml.CZ(wires=[0, 2])
          qml.RY(0.1, wires=[1])
          qml.PauliZ(wires=[0])
          qml.CNOT(wires=[0, 2])
          qml.PauliZ(wires=[0])
          qml.CNOT(wires=[1, 4])
          qml.PauliY(wires=1)
          qml.PauliZ(wires=0)
          qml.CZ(wires=[0, 2])
          qml.Toffoli(wires=[0, 2, 4])
          return qml.expval(qml.PauliX(wires=0))
  
      with qml.queuing.AnnotatedQueue() as q_template:
          qml.Toffoli(wires=[0, 1, 2])
          qml.CNOT(wires=[0, 1])
          qml.Toffoli(wires=[0, 1, 2])
          qml.CNOT(wires=[0, 1])
          qml.CNOT(wires=[0, 2])
  
      template = qml.tape.QuantumScript.from_queue(q_template)
      dev = qml.device("default.qubit", wires=5)
  
      qnode = qml.QNode(circuit, dev)
      qnode()
  
      optimized_qfunc = pattern_matching_optimization(
          circuit, pattern_tapes=[template]
      )
      optimized_qnode = qml.QNode(optimized_qfunc, dev)
      optimized_qnode()
  
      toffolis_qnode = qml.specs(qnode)()["resources"].gate_types[
          "Toffoli"
      ]
      toffolis_optimized_qnode = qml.specs(optimized_qnode)()[
          "resources"
      ].gate_types["Toffoli"]
  
      assert len(qnode.qtape.operations) == 11
      assert toffolis_qnode == 2
  
      assert len(optimized_qnode.qtape.operations) == 10
      assert toffolis_optimized_qnode == 0
  
      assert np.allclose(
          qml.matrix(optimized_qnode)(), qml.matrix(qnode)()
      )
  
imports: import pytest
  import pennylane
  import pennylane.numpy
  from pennylane.transforms.commutation_dag import commutation_dag
  from pennylane.transforms.optimization.pattern_matching import pattern_matching, pattern_matching_optimization, ForwardMatch, BackwardMatch, _update_qubits
label_desc: Use image node_id 6 for calling the TestPatternMatchingOptimization obj's underlying member method code with example usage: obj.test_template_with_toffoli() and returns: qml