Skip to content

Commit

Permalink
Merge pull request #43 from ChitambarLab/v0.1.4-int
Browse files Browse the repository at this point in the history
V0.1.4 int
  • Loading branch information
bdoolittle authored Jul 24, 2022
2 parents d91dba3 + c646de2 commit 8a3ac3d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CITATION.bib
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ @misc{qNetVO
title = {qNetVO: the Quantum Network Variational Optimizer},
howpublished = {\url{https://github.com/ChitambarLab/qNetVO}},
url = {https://github.com/ChitambarLab/qNetVO},
version = {v0.1.3},
version = {v0.1.4},
year = {2022},
month = {March},
doi = {10.5281/zenodo.6345834},
Expand Down
2 changes: 2 additions & 0 deletions docs/source/cost/entropic_quantities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Entropic Quantities

.. currentmodule:: qnetvo

.. autofunction:: shannon_entropy_cost_fn

.. autofunction:: mutual_info_cost_fn


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = qNetVO
version = 0.1.3
version = 0.1.4
author = Brian Doolittle and Tom Bromley
author_email = [email protected]
description = The Quantum Network Variational Optimizer
Expand Down
1 change: 1 addition & 0 deletions src/qnetvo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .version import __version__
from qnetvo.utilities import *
from qnetvo.network_ansatz import *
from qnetvo.ansatz_library import *
Expand Down
40 changes: 40 additions & 0 deletions src/qnetvo/cost/mutual_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,43 @@ def cost_fn(scenario_settings):
return -(mutual_info)

return cost_fn


def shannon_entropy_cost_fn(ansatz, **qnode_kwargs):
"""Constructs an ansatz-specific Shannon entropy cost function
The Shannon entropy characterizes the amount of randomness, or similarly, the amount of
information is present in a random variable. Formally, let :math:'X' be a discrete random
variable, then the Shannon entropy is defined by the expression:
.. math::
H(X) = \\sum_{x} P(x) \\log_{2} P(x)
In the case of a quantum network, the Shannon entropy is defined on the measurement outcome
of the network ansatz.
:param ansatz: The ansatz circuit on which the Shannon entropy is evalutated.
:type ansatz: NetworkAnsatz
:param qnode_kwargs: Keyword arguments passed to the execute qnodes.
:type qnode_kwargs: dictionary
:returns: A cost function ``shannon_entropy(scenario_settings)`` parameterized by
the ansatz-specific scenario settings.
:rtype: Function
"""
num_prep_nodes = len(ansatz.prepare_nodes)
num_meas_nodes = len(ansatz.measure_nodes)

probs_qnode = joint_probs_qnode(ansatz, **qnode_kwargs)

def cost_fn(scenario_settings):
settings = ansatz.qnode_settings(
scenario_settings, [0] * num_prep_nodes, [0] * num_meas_nodes
)
probs_vec = probs_qnode(settings)

return shannon_entropy(probs_vec)

return cost_fn
3 changes: 3 additions & 0 deletions src/qnetvo/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg_resources

__version__ = pkg_resources.get_distribution(__name__.split(".")[0]).version
39 changes: 39 additions & 0 deletions test/cost/mutual_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,42 @@ def test_mutual_info_opt_qubit_33(self, priors, match):
)

assert np.isclose(opt_dict["scores"][-1], match, atol=0.0005)


class TestShannonEntropy:
def test_shannon_entropy_pure_state(self):
np.random.seed(123)

prep_node = [qnet.PrepareNode(1, [0, 1], qnet.ghz_state, 0)]
meas_node = [qnet.MeasureNode(1, 4, [0, 1], qml.ArbitraryUnitary, 4**2 - 1)]

ansatz = qnet.NetworkAnsatz(prep_node, meas_node)
shannon_entropy = qnet.shannon_entropy_cost_fn(ansatz)

settings = ansatz.rand_scenario_settings()
opt_dict = qnet.gradient_descent(
shannon_entropy, settings, step_size=0.08, sample_width=5, num_steps=30
)

assert np.isclose(opt_dict["scores"][-1], 0, atol=0.0005)

def test_von_neumann_entropy_mixed_state(self):
np.random.seed(123)

prep_node = [qnet.PrepareNode(1, [0, 1], qnet.ghz_state, 0)]
meas_node = [qnet.MeasureNode(1, 4, [0, 1], qml.ArbitraryUnitary, 4**2 - 1)]
gamma = 0.04
noise_node = [
qnet.NoiseNode([0], lambda settings, wires: qml.DepolarizingChannel(gamma, wires)),
qnet.NoiseNode([1], lambda settings, wires: qml.DepolarizingChannel(gamma, wires)),
]

ansatz = qnet.NetworkAnsatz(prep_node, meas_node, noise_node)
shannon_entropy = qnet.shannon_entropy_cost_fn(ansatz)

settings = ansatz.rand_scenario_settings()
opt_dict = qnet.gradient_descent(
shannon_entropy, settings, step_size=0.1, sample_width=5, num_steps=30
)

assert np.isclose(opt_dict["scores"][-1], -0.518, atol=0.0005)
11 changes: 11 additions & 0 deletions test/version_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import re

import qnetvo


def test_version():

version_str = qnetvo.__version__

assert isinstance(version_str, str)
assert re.fullmatch(r"0\.1\.\d+", version_str) != None

0 comments on commit 8a3ac3d

Please sign in to comment.