I have recently started looking at the qubit tapering and its application in quantum chemistry. Is there anyway to count the number of CNOT gates or Pauli gates in a given Hamiltonian. So, it can be used to compare the no. of gates in a Hamiltonian before and qubit tapering.

Hi @raghavv, I donâ€™t think we have a gate-counting function but you could export your circuit to qasm and then count the gates from there. Hereâ€™s an example of how to export a circuit to qasm:

I am not quiet sure, how to print the no. of gates involved. Actually, I want to know how many Pauli Gates are there for the Hamiltonian of a system. For eg. the Hamiltonian for H2 molecule can be obtained as.

symbols = [â€śHâ€ť, â€śHâ€ť]
x = np.array([8.14341433,1.84432727,0.00000000,
5.25213336,1.84432727,0.00000000]
, requires_grad=True)

Hamil = qml.qchem.molecular_hamiltonian(symbols, x,
active_electrons=active_electrons,
active_orbitals=active_MOs,
basis=basis
)[0]
So now , there are many attributes to the Hamil such as

Hamil.coeffs [give coefficients of Pauli gates acting on each qubit]

And looks like these attributes print the Pauli Operators. I just want to know if there is a way to count these operators like, how many Pauli(X,Y,Z) etc.

Hi @raghavv. I asked a colleague about this and he put together a function that does what you need. It might not be super stable but it seems to work. You can modify it to count a specific Pauli operator if thatâ€™s what you need.

from pennylane.ops.qubit.non_parametric_ops import PauliX, PauliY, PauliZ
from pennylane.operation import Tensor
def count_paulis(H):
"""
Counts the number of Pauli operations in a Hamiltonian. Note that each Pauli
operator contained in tensor products are counted individually e.g:
qml.PauliX(0) @ qml.PauliX(1) counts as 2. Identity operations are not
counted as Pauli operations here.
Args:
H (qml.Hamiltonian): The Hamiltonian containing operations to be counted
Returns:
count (int): the number of Pauli operations contained in the Hamiltonian
"""
pauli_types = (PauliX, PauliY, PauliZ)
count = 0
for op in H.ops:
if type(op) == Tensor:
for obs in op.obs:
if type(obs) in pauli_types:
count += 1
elif type(op) in pauli_types:
count += 1
return count

Hi @CatalinaAlbornoz. Thank you so much for asking your colleague and thanks for sharing the code. Thatsâ€™s so nice of you. It works, this is the kind I was looking for.

On the other hand, I also tried my hand with shell, the following line counted the Pauli gates (X,Y,Z). Just sharing.
grep -o PauliZ Pauli_Gates.txt | wc -l

Hi @CatalinaAlbornoz. One can run the command Hamiltonian.ops and then list the Pauli gates and save it to a text file. And the run the grep command (grep -o PauliZ Pauli_Gates.txt | wc -l). Hope that is clear.