qiskit-community / qiskit-nature

Qiskit Nature is an open-source, quantum computing, framework for solving quantum mechanical natural science problems.
https://qiskit-community.github.io/qiskit-nature/
Apache License 2.0
299 stars 202 forks source link

Method for finding amount of iterations and measurements of a VQE #437

Closed HehnLukas closed 2 years ago

HehnLukas commented 2 years ago

Hi, I'm relatively new to QC so I'm sorry if there is something obvious I am missing. I would like to find out how many measurements and gates a VQE takes for the execution. For example when I run the code given in the readme, but with the two_qubit_reduction set to False, I get this ansatz:

        ┌───┐    ┌──────────┐┌──────────┐            ┌──────────┐┌───────────┐                                   ┌───────────┐┌───────────┐                                   ┌───────────┐┌───────────┐»
q_0: ───┤ X ├────┤ Ry(θ[0]) ├┤ Rz(θ[4]) ├─■──■─────■─┤ Ry(θ[8]) ├┤ Rz(θ[12]) ├───────────────────■───────■─────■─┤ Ry(θ[16]) ├┤ Rz(θ[20]) ├───────────────────■───────■─────■─┤ Ry(θ[24]) ├┤ Rz(θ[28]) ├»
        ├───┤    ├──────────┤├──────────┤ │  │     │ └──────────┘└┬──────────┤┌───────────┐      │       │     │ └───────────┘├───────────┤┌───────────┐      │       │     │ └───────────┘├───────────┤»
q_1: ───┤ X ├────┤ Ry(θ[1]) ├┤ Rz(θ[5]) ├─■──┼──■──┼──────■───────┤ Ry(θ[9]) ├┤ Rz(θ[13]) ├──────■───────┼──■──┼───────■──────┤ Ry(θ[17]) ├┤ Rz(θ[21]) ├──────■───────┼──■──┼───────■──────┤ Ry(θ[25]) ├»
     ┌──┴───┴───┐├──────────┤└──────────┘    │  │  │      │       └──────────┘├───────────┤┌───────────┐ │  │  │       │      └───────────┘├───────────┤┌───────────┐ │  │  │       │      └───────────┘»
q_2: ┤ Ry(θ[2]) ├┤ Rz(θ[6]) ├────────────────■──■──┼──────┼────────────■──────┤ Ry(θ[10]) ├┤ Rz(θ[14]) ├─■──■──┼───────┼────────────■──────┤ Ry(θ[18]) ├┤ Rz(θ[22]) ├─■──■──┼───────┼────────────■──────»
     ├──────────┤├──────────┤                      │      │            │      ├───────────┤├───────────┤       │       │            │      ├───────────┤├───────────┤       │       │            │      »
q_3: ┤ Ry(θ[3]) ├┤ Rz(θ[7]) ├──────────────────────■──────■────────────■──────┤ Ry(θ[11]) ├┤ Rz(θ[15]) ├───────■───────■────────────■──────┤ Ry(θ[19]) ├┤ Rz(θ[23]) ├───────■───────■────────────■──────»
     └──────────┘└──────────┘                                                 └───────────┘└───────────┘                                   └───────────┘└───────────┘                                   » 

If I understand correctly, each R(θ) is a classical parameter optimization which requires a measurement. But I have no idea how to count the measurements and iterations the VQE does. I've tried using ansatz.depth() which gives 2, and ansatz.count_ops() gives this dict: {'x': 2, 'TwoLocal': 1}, but that is not what I'm looking for.

mrossinek commented 2 years ago

Hi there!

There are multiple questions here so I will try to answer them one by one:

I hope this answers most of your questions. May I also suggest the Qiskit Slack workspace to you? That is usually a faster way of getting answers to more general questions like this one.