Skip to content

Conversation

@LuisAlfredoNu
Copy link
Contributor

@LuisAlfredoNu LuisAlfredoNu commented Feb 13, 2025

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    tests directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • Ensure that code is properly formatted by running make format.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
Currently, LT only accepts MPS with the following bond dimensions:

[ [2,2] [2,2,4] [4,2,8] [8,2,16] .... [16,2,8] [8,2,4] [4,2,2] [2,2]]

Which is a limitation of MPSPrep gate because it is not possible to pass custom MPS from other softwares that works with different MPS layouts

Description of the Change:
Remove the checks regarding MPS bond dimension and define the device argument bond_dim to pass the new bond dimension that will be used in the device initializer to allocate the a custom MPS into the GPU.

An example of how to use it:

wires = 4
bond_dims = [5,7,5]

MPS_example = create_MPS_with_custom_bondDims(wires, bond_dims)

dev = qml.device("lightning.tensor", 
                 wires=wires, 
                 bond_dim=bond_dims # New device argument that only accepts a list of n_qubits-1 element 
                 )

dev_wires = dev.wires.tolist()

@qml.qnode(dev)
def circuit(custom_MPS=MPS_example):
    
    qml.MPSPrep(mps=custom_MPS, wires=dev_wires)
    # circuit content 
    # ...
    
    return qml.expval(qml.PauliZ(0))

Benefits:

Possible Drawbacks:
The MPS shape should be symmetric, i.e. [[2,n] [n,2,m] ... [m,2,n] [n,2]]

Related GitHub Issues:
[sc-83835]

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link
Member

@multiphaseCFD multiphaseCFD left a comment

Choose a reason for hiding this comment

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

Thanks @LuisAlfredoNu for the nice work!

}

const auto &MPS_shape_dest = tensor_network.getSitesExtents();
// print MPS_shape_dest and MPS_shape_source
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// print MPS_shape_dest and MPS_shape_source

Comment on lines +158 to +172
std::cout << "MPS_shape_dest: " << std::endl;
for (const auto &shape : MPS_shape_dest) {
for (const auto &dim : shape) {
std::cout << dim << " ";
}
std::cout << std::endl;
}

std::cout << "MPS_shape_source: " << std::endl;
for (const auto &shape : MPS_shape_source) {
for (const auto &dim : shape) {
std::cout << dim << " ";
}
std::cout << std::endl;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
std::cout << "MPS_shape_dest: " << std::endl;
for (const auto &shape : MPS_shape_dest) {
for (const auto &dim : shape) {
std::cout << dim << " ";
}
std::cout << std::endl;
}
std::cout << "MPS_shape_source: " << std::endl;
for (const auto &shape : MPS_shape_source) {
for (const auto &dim : shape) {
std::cout << dim << " ";
}
std::cout << std::endl;
}


_device_options = {
"mps": ("backend", "max_bond_dim", "cutoff", "cutoff_mode"),
"mps": ("backend", "max_bond_dim", "cutoff", "cutoff_mode", "bond_dim"),
Copy link
Member

Choose a reason for hiding this comment

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

Would you please update the docstr to add the new arg?

self._method,
self._c_dtype,
device_name=self.name,
max_bond_dim=self._max_bond_dim,
Copy link
Member

Choose a reason for hiding this comment

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

just wondering how max_bond_dim would work in the background if _bond_dim.

if self._cutoff_mode not in ["rel", "abs"]:
raise ValueError(f"Unsupported cutoff mode: {self._cutoff_mode}")

if self._bond_dim is not None:
Copy link
Member

Choose a reason for hiding this comment

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

looks good as long as it works for algo and product. it would be even better to late initialize TN object util MPSprep is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea, but I am afraid of the potential risk of skipping multiple safety checks because the only way to update the TN object until the call of qml.MPSPrep should be destroy the current MPS and then create a new one with the new bond dims 😕

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.

4 participants