qiskit
qiskit copied to clipboard
Entangling capacity as an Ansatz quality metric (resolves #8176)
Summary
Resolves #8176
Details and comments
Need to add label: Changelog: New Feature to the PR
Thank you for opening a new pull request.
Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.
While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.
One or more of the the following people are requested to review this:
- @Qiskit/terra-core
from qiskit.utils.circuit_utils import Entanglement
from qiskit.utils.entanglement.parametric_circuits import Ansatz
import numpy as np
from qiskit import Aer
total_circuit = 19 #for iterating over every given 19 parametric circuit in Ansatz
feature_dim = 4 #no of qubits
repitition = 1 #no. of layers in the ansatz
num_eval = 1 #total number of time we need to run the measure
meyer_wallach_measure = np.zeros((total_circuit, repitition))
von_neumann_measure = np.zeros((total_circuit, repitition))
backend = Aer.get_backend("statevector_simulator")
for k in range(num_eval):
for i in range(total_circuit):
circuit_id = i + 1
for layers in range(repitition):
ansatze = Ansatz(layers + 1, feature_dim, circuit_id).get_ansatz()
mw_cap = Entanglement(ansatze, backend, num_params=100).get_entanglement()
vn_cap = Entanglement(ansatze, feature_dim, ent_measure="von-neumann").get_entanglement()
meyer_wallach_measure[i, layers] = mw_cap
von_neumann_measure[i, layers] = vn_cap
file_name = "mw_entropy_run" + str(k)
np.savetxt(file_name + ".csv", meyer_wallach_measure, delimiter="\t")
file_name = "vn_entropy_run" + str(k)
np.savetxt(file_name + ".csv", von_neumann_measure, delimiter="\t")
To test the new module we can use the above python script to get the default entanglement measure using meyer-wallach measure of parametric circuit 1 given in the article https://arxiv.org/pdf/1905.10876.pdf
Pull Request Test Coverage Report for Build 2567278805
- 43 of 454 (9.47%) changed or added relevant lines in 6 files are covered.
- 101 unchanged lines in 8 files lost coverage.
- Overall coverage decreased (-0.5%) to 83.445%
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
---|---|---|---|
qiskit/utils/entanglement/meyer_wallach_measure.py | 3 | 11 | 27.27% |
qiskit/utils/entanglement/von_neumann_entropy.py | 3 | 11 | 27.27% |
qiskit/utils/circuit_utils.py | 7 | 40 | 17.5% |
qiskit/utils/entanglement/parametric_circuits.py | 25 | 387 | 6.46% |
<!-- | Total: | 43 | 454 |
Files with Coverage Reduction | New Missed Lines | % |
---|---|---|
qiskit/exceptions.py | 1 | 95.45% |
qiskit/extensions/unitary.py | 1 | 95.92% |
qiskit/quantum_info/operators/symplectic/base_pauli.py | 1 | 87.13% |
qiskit/pulse/library/waveform.py | 3 | 89.36% |
qiskit/qpy/interface.py | 6 | 90.32% |
qiskit/quantum_info/operators/symplectic/clifford.py | 14 | 88.15% |
qiskit/quantum_info/operators/dihedral/dihedral.py | 26 | 79.03% |
qiskit/quantum_info/operators/symplectic/pauli.py | 49 | 80.23% |
<!-- | Total: | 101 |
Totals | |
---|---|
Change from base Build 2546341916: | -0.5% |
Covered Lines: | 55786 |
Relevant Lines: | 66854 |
💛 - Coveralls
One aspect that seems to be missing so far are unit tests for the new function that is introduced.
One aspect that seems to be missing so far are unit tests for the new function that is introduced.
@woodsp-ibm yes so the testing code was somehow throwing errors with tex -edocs so we removed it and tried and everything worked so kept it out of utils.. this is the testing code to test the new feature we are trying to add. specifically the two lines with comments in the below code are to test the two functions added.
from qiskit.utils.circuit_utils import Entanglement
from qiskit.utils.entanglement.parametric_circuits import Ansatz
import numpy as np
from qiskit import Aer
total_circuit = 19 #for iterating over every given 19 parametric circuit in Ansatz
feature_dim = 4 #no of qubits
repitition = 1 #no. of layers in the ansatz
num_eval = 1 #total number of time we need to run the measure
meyer_wallach_measure = np.zeros((total_circuit, repitition))
von_neumann_measure = np.zeros((total_circuit, repitition))
backend = Aer.get_backend("statevector_simulator")
for k in range(num_eval):
for i in range(total_circuit):
circuit_id = i + 1
for layers in range(repitition):
ansatze = Ansatz(layers + 1, feature_dim, circuit_id).get_ansatz()
mw_cap = Entanglement(ansatze, backend, num_params=100).get_entanglement() #tests function Meyer Wallach
vn_cap = Entanglement(ansatze, feature_dim, ent_measure="von-neumann").get_entanglement() #tests function von_neumann
meyer_wallach_measure[i, layers] = mw_cap
von_neumann_measure[i, layers] = vn_cap
file_name = "mw_entropy_run" + str(k)
np.savetxt(file_name + ".csv", meyer_wallach_measure, delimiter="\t")
file_name = "vn_entropy_run" + str(k)
np.savetxt(file_name + ".csv", von_neumann_measure, delimiter="\t")
Tests should be in the form of Python units that assert correctness of result etc. See the many examples here https://github.com/Qiskit/qiskit-terra/tree/main/test/python for various aspects. Unit tests are run by CI to ensure the code works, and continues to work through changes etc, as expected.
Reproducing and checking the numbers from the circuit expressitivity paper is very interesting!
However we're not quite sure how this contribution should be integrated to Qiskit yet.
-
If we include it in Qiskit, the best place for the entanglement measures would likely be the
qiskit.quantum_info
module. Then, the code would have to work with normal Qiskit circuits (not new defined, analogous classes) and we would not be able to include all the pre-constructed circuits. If some of them turn out to be very useful, we could potentially add them to thecircuit.library
module. - Alternatively, we could keep your code in a third party repository. There you would have full freedom of how to structure the code, keep all the measures, circuits etc.
Depending on how much you want to change the current code or whether you want to keep it as is, either of the solutions might be better 🙂
@Cryoris the code works for any ansatz and can be used for VQE or VQC or any variational quantum algorithm. So its pretty generic and the only input it needs is a quantum circuit so we kept it under utils.
we have put in 19 types of ansatz that we have tested on..u don't need to include any of the constructed circuits.. we added that to provide a mechanism to test the function. the testing code give in the comment above uses that reconstructed circuit code. barring that we don't need that code at all. We are working on creating unit tests as pointed by Steve and will be try to push them asap after the tox tests. Thanks
@woodsp-ibm the unit test is in the format expected and works fine. its pushed now and all tests passed. Thanks
@woodsp-ibm @HuangJunye please let me know if you need any information or any codes or anything to help move this PR. Thanks
@anupamaray While I was able to help you with procedure/mechanics for the PR the material is not a subject area I can technically review the content. Someone from terra-core team will need to do this.
We discussed this in the team and it would be great to add the von Neumann and Meyer-Wallach entanglement measures to quantum info. There's an existing file qiskit/quantum_info/states/measures.py
which already contains similar functions. There you could add the entanglement measures as two new standalone functions, but we think that it would be best to not add the Entanglement
and Ansatz
classes to Qiskit Terra.
I think your full code, with all the ansatz circuits, would be a great contribution to the qiskit-community repository 🙂
Thanks @Cryoris for your time and feedback. we went through qiskit/quantum_info/states/measures.py
but here all functions accept only statevectors and not circuits.
for our code we want to compute the entangling capacity of an ansatz so we pass the quantum circuit. for example the function we will add to measures.py will look like:
def entanglement_von_neumann (qc): Args: qc: a quantum circuit. Returns: float: The von-Neumann entanglement.
Is this okay or we need to find some other code where u have functions taking a quantum circuit as input. Kindly let us know. Thanks a ton for your help and feedback.
I think you could have the function accept both a statevector or a quantum circuit, since you anyways convert the circuit into a statevector, right? Something like
def von_neumann_entropy(data):
if not isinstance(data, Statevector):
data = Statevector(data)
You could also add a second argument for the number of parameter sets you want to average over in case your circuit is parameterized, maybe like
def von_neumann_entropy(data, num_parameter_samples=None):
if isinstance(data, QuantumCircuit) and data.num_parameters > 0:
parameter_sets = [np.random.random(data.num_parameters) for _ in range(num_parameter_samples)]
return np.mean(von_neumann_entropy(data.bind_parameters(params) for params in parameter_sets))
if not isinstance(data, Statevector):
data = Statevector(data)
....
@anupamaray are you still pursuing this? I think this would be a great addition in the format we discussed above! 🙂
yes Julien, we have cleaned up and put in this format, but then some lint tests failed. I ll get to it asap and resubmit it. Thanks a ton for your feedback and support.
hi @anupamaray re you still working on this?
This work eventually ended up being contributed as a tutorial to the community https://github.com/qiskit-community/qiskit-community-tutorials/pull/125 as suggested above https://github.com/Qiskit/qiskit-terra/pull/8226#issuecomment-1203627584
As this became a tutorial I am closing this as complete.