-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
negativity_cost_fn and density qnode functions #53
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Partial Transpose Negativity | ||
============================ | ||
|
||
.. currentmodule:: qnetvo | ||
|
||
.. autofunction:: negativity_cost_fn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pennylane as qml | ||
from pennylane import math | ||
import numpy as np | ||
from ..qnodes import density_matrix_qnode | ||
from ..utilities import partial_transpose | ||
|
||
|
||
def negativity_cost_fn(network_ansatz, m, n, wires, qnode_kwargs={}): | ||
"""Constructs an ansatz-specific negativity cost function. | ||
|
||
Negativity can be used to identify if two subsystems :math:`A` and :math:`B` are | ||
entangled, through the PPT criterion. Negativity is an upper bound for distillable entanglement. | ||
|
||
This entanglement measure is expressed as | ||
|
||
.. math:: | ||
|
||
\\mathcal{N}(\\rho) = |\\sum_{\\lambda_i < 0}\\lambda_i|, | ||
|
||
where :math:`\\rho^{\\Gamma_B}` is the partial transpose of the joint state with respect to | ||
the :math:`B` party, and :math:`\\lambda_i` are all of the eigenvalues of :math:`\\rho^{\\Gamma_B}`. | ||
|
||
For more information on negativity and its applications in quantum information theory, | ||
(see `Vidal and Werner, 2001 <https://arxiv.org/pdf/quant-ph/0102117>`_). | ||
|
||
:param ansatz: The ansatz circuit on which the negativity is evaluated. | ||
:type ansatz: NetworkAnsatz | ||
|
||
:param m: The size of the :math:`A` subsystem. | ||
:type m: int | ||
|
||
:param n: The size of the :math:`B` subsystem. | ||
:type n: int | ||
|
||
:param wires: The wires which define the joint state. | ||
:type wires: list[int] | ||
|
||
:param qnode_kwargs: Keyword arguments passed to the execute qnodes. | ||
:type qnode_kwargs: dictionary | ||
|
||
:returns: A cost function ``negativity_cost(*network_settings)`` parameterized by | ||
the ansatz-specific scenario settings. | ||
:rtype: Function | ||
|
||
:raises ValueError: If the sum of the sizes of the two subsystems (``m + n``) does not match the length of ``wires``. | ||
""" | ||
|
||
if len(wires) != m + n: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
raise ValueError(f"Sum of sizes of two subsystems should be {len(wires)}; got {m+n}.") | ||
|
||
density_qnode = density_matrix_qnode(network_ansatz, wires, **qnode_kwargs) | ||
|
||
def negativity_cost(*network_settings): | ||
dm = density_qnode(network_settings) | ||
dm_pt = partial_transpose(dm, 2**m, 2**n) | ||
eigenvalues = math.eigvalsh(dm_pt) | ||
negativity = np.sum(np.abs(eigenvalues[eigenvalues < 0])) | ||
return -negativity | ||
|
||
return negativity_cost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
from pennylane import numpy as np | ||
|
||
import qnetvo as qnet | ||
|
||
|
||
class TestNegativityCost: | ||
def test_negativity_cost_fn(self): | ||
|
||
prep_nodes = [ | ||
qnet.PrepareNode(1, [0, 1], qnet.bell_state_copies, 2), | ||
] | ||
|
||
ansatz = qnet.NetworkAnsatz(prep_nodes) | ||
|
||
negativity_cost = qnet.negativity_cost_fn(ansatz, m=1, n=1, wires=[0, 1]) | ||
|
||
zero_settings = ansatz.zero_network_settings() | ||
|
||
negativity_value = negativity_cost(*zero_settings) | ||
|
||
expected_negativity = -0.5 | ||
assert np.isclose( | ||
negativity_value, expected_negativity | ||
), f"Expected {expected_negativity}, but got {negativity_value}" | ||
|
||
separable_prep_nodes = [ | ||
qnet.PrepareNode(4, [0, 1], qnet.local_RY, 2), | ||
] | ||
|
||
separable_ansatz = qnet.NetworkAnsatz(separable_prep_nodes) | ||
|
||
separable_negativity_cost = qnet.negativity_cost_fn( | ||
separable_ansatz, m=1, n=1, wires=[0, 1] | ||
) | ||
|
||
separable_negativity_value = separable_negativity_cost(*zero_settings) | ||
|
||
expected_separable_negativity = 0 | ||
assert np.isclose( | ||
separable_negativity_value, expected_separable_negativity | ||
), f"Expected {expected_separable_negativity}, but got {separable_negativity_value}" | ||
|
||
with pytest.raises(ValueError, match="Sum of sizes of two subsystems should be"): | ||
qnet.negativity_cost_fn(ansatz, m=2, n=1, wires=[0, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
:raises ValueError:
in the docstring to document the constraints onm
andn
As an example, you can see docstring for the
qnetvo.NetworkAnsatz
class.