Skip to content
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

Added method to reshape dims of network. #9

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

AGaliciaMartinez
Copy link
Member

@AGaliciaMartinez AGaliciaMartinez commented Dec 29, 2021

This PR implements two methods, match_in_dims and match_out_dims that allows splitting edges to match a target dimension. This is primarily meant to be used with ptrace as we need to reshape the outer nodes such that the network has the appropriate dimensions.

Note that _match_edges_by_split is a very similar function but with different scope. The new _match_dimensions is meant to be used with unary operators that required a change in the dimension (ptrace) whereas _match_edges_by_split is meant to be used with binary operators (matmul). This is because for _match_dimensions (unary version) we do not allow merging two edge dimension. However, such operation can also be achieved in _match_edges_by_split (binary version) by just splitting the edges as two list of edges is available.

The reason to not allow here in _match_dimensions to merge edges is because this is a relatively complex operation that will require contracting nodes. I am assuming that it is best to do the contraction of the full network in a single step such that an optimized path is used.

@coveralls
Copy link

coveralls commented Dec 29, 2021

Pull Request Test Coverage Report for Build 1693744455

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 40 of 41 (97.56%) changed or added relevant lines in 1 file are covered.
  • 11 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+2.4%) to 93.333%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/qutip_tensornetwork/core/data/network.py 40 41 97.56%
Files with Coverage Reduction New Missed Lines %
src/qutip_tensornetwork/core/data/network.py 11 91.42%
Totals Coverage Status
Change from base Build 1633978418: 2.4%
Covered Lines: 434
Relevant Lines: 465

💛 - Coveralls

@AGaliciaMartinez AGaliciaMartinez marked this pull request as ready for review December 29, 2021 16:21
@AGaliciaMartinez AGaliciaMartinez changed the title Dims compatible Added method to reshape dims of network. Dec 29, 2021
Copy link
Contributor

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good, but I left some questions about the handling of the error cases.

src/qutip_tensornetwork/core/data/network.py Outdated Show resolved Hide resolved
src/qutip_tensornetwork/core/data/network.py Outdated Show resolved Hide resolved
src/qutip_tensornetwork/core/data/network.py Outdated Show resolved Hide resolved
src/qutip_tensornetwork/core/data/network.py Outdated Show resolved Hide resolved
src/qutip_tensornetwork/core/data/network.py Outdated Show resolved Hide resolved
src/qutip_tensornetwork/core/data/network.py Show resolved Hide resolved
tests/core/data/test_network.py Outdated Show resolved Hide resolved
tests/core/data/test_network.py Outdated Show resolved Hide resolved
tests/core/data/test_network.py Outdated Show resolved Hide resolved
node = random_node(dim_edges)
network = Network(node[:], [], copy=False)
with pytest.raises(ValueError):
network.match_out_dims(target_dims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worthwhile asserting that network is in a valid state after an error has been raised.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I will do so.

@AGaliciaMartinez
Copy link
Member Author

Thank you for the review Simon!

@AGaliciaMartinez
Copy link
Member Author

I changed so that match_in_dims and match_out_dims return a new Network instead of performing an inplace modification. @hodgestar, could you take a look at the latest changes?

Copy link
Contributor

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple more suggestions. Thank you for sorting out the state of the network post errors by copying the network -- I think that was a good solution.

Comment on lines +581 to +582
``target_dims``. After this function the following will hold:
``network.dims[0] == target_dims``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``target_dims``. After this function the following will hold:
``network.dims[0] == target_dims``
``target_dims``. The returned network will have ``network.dims[0] == target_dims``.

Comment on lines +619 to +620
``target_dims``. After this function the following will hold:
``network.dims[1] == target_dims``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``target_dims``. After this function the following will hold:
``network.dims[1] == target_dims``
``target_dims``. The returned network will have ``network.dims[1] == target_dims``.

Parameters
----------
target_dims: list of int
Desired dimensions for ``out_edges``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Desired dimensions for ``out_edges``.
Desired dimensions for ``in_edges``.

>>> array = np.random.random(network_dim)
>>> node = tn.Node(array)
>>> network = Network(node[:], [])
>>> network = network.match_out_dims(target_dims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> network = network.match_out_dims(target_dims)
>>> network = network.match_in_dims(target_dims)

new_edges = []

if len(edges) == 0 and len(target_dims) == 0:
return _edges
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be return edges? _edges does not seem to be defined any more. We should add a test for the empty edges and target_dims case.

Comment on lines +800 to +803
"edges are not compatible. The dimensions for edges is "
+ str(e_dims)
+ ", whereas the target dimension is"
+ str(_target_dims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"edges are not compatible. The dimensions for edges is "
+ str(e_dims)
+ ", whereas the target dimension is"
+ str(_target_dims)
f"edges are not compatible. The dimensions for edges is {e_dims}"
f", whereas the target dimension is {_target_dims}."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should just use f-strings these days.


e_dims = [e.dimension for e in edges]

new_edges = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the initialization of new_edges to just before the top of the loop so that it's closer to where it's used. Currently we define it, then don't use it for awhile, then suddenly use it in the middle of a loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants