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

Cu quantum mps #17

Merged
merged 31 commits into from
Oct 17, 2023
Merged

Cu quantum mps #17

merged 31 commits into from
Oct 17, 2023

Conversation

Tankya2
Copy link
Contributor

@Tankya2 Tankya2 commented Jul 25, 2023

Hi this is to address issue #15. Added conversion of Qibo circuit form to MPS format and provided method to contract to state vector format.

@Tankya2 Tankya2 requested review from liweintu and scarrazza July 25, 2023 02:36
@liweintu liweintu requested a review from alecandido July 25, 2023 02:39
Copy link
Member

@alecandido alecandido left a comment

Choose a reason for hiding this comment

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

To be fair, I should sit down some more time to check that all operations in the helper make sense (writing them on a piece of paper and reading the cuQuantum API). But they seem reasonable (index attribution is alright, but for more complex operations I should dedicate some more time).

The main concern I have is about the cache usage, but everything else is rather slim and sensible.

I was trying to understand if it's possible to simplify the code in contract_expectation, but I'm not sure we could do much better.

src/qibotn/MPSContractionHelper.py Outdated Show resolved Hide resolved
src/qibotn/MPSContractionHelper.py Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
src/qibotn/MPSContractionHelper.py Outdated Show resolved Hide resolved
src/qibotn/MPSUtils.py Outdated Show resolved Hide resolved
src/qibotn/MPSUtils.py Outdated Show resolved Hide resolved
src/qibotn/QiboCircuitToMPS.py Outdated Show resolved Hide resolved
src/qibotn/MPSUtils.py Outdated Show resolved Hide resolved
src/qibotn/cutn.py Outdated Show resolved Hide resolved
src/qibotn/cutn.py Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
.github/workflows/rules.yml Show resolved Hide resolved
@liweintu liweintu requested a review from alecandido September 12, 2023 04:20
@liweintu
Copy link
Contributor

Reporting an issue here.

If using the latest version of qibo (0.2.0), this PR (commit f59b1b0) hit an error below.

File "/home/liwei/repos/qibotn/src/qibotn/cutn.py", line 57, in
sv_mpi, rank = eval_tn_MPI(circ_qibo, dtype)
File "/home/liwei/repos/qibotn/src/qibotn/cutn.py", line 32, in eval_tn_MPI
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/qibotn-0.0.1.dev0-py3.9.egg/qibotn/QiboCircuitConvertor.py", line 23, in init
self.init_intermediate_circuit(circuit)
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/qibotn-0.0.1.dev0-py3.9.egg/qibotn/QiboCircuitConvertor.py", line 102, in init_intermediate_circuit
cp.asarray(gates.FusedGate(gate).matrix, dtype=self.dtype).reshape(
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/cupy/_creation/from_data.py", line 76, in asarray
return _core.array(a, dtype, False, order)
File "cupy/_core/core.pyx", line 2360, in cupy._core.core.array
File "cupy/_core/core.pyx", line 2384, in cupy._core.core.array
File "cupy/_core/core.pyx", line 2509, in cupy._core.core._array_default
TypeError: must be real number, not method

It seems that the gate.matrix() return value is not compatible with cupy.asarray() any more. We need to resolve this, maybe in another PR? @alecandido

@liweintu
Copy link
Contributor

liweintu commented Sep 27, 2023

Reporting an issue here.

If using the latest version of qibo (0.2.0), this PR (commit f59b1b0) hit an error below.

File "/home/liwei/repos/qibotn/src/qibotn/cutn.py", line 57, in
sv_mpi, rank = eval_tn_MPI(circ_qibo, dtype)
File "/home/liwei/repos/qibotn/src/qibotn/cutn.py", line 32, in eval_tn_MPI
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/qibotn-0.0.1.dev0-py3.9.egg/qibotn/QiboCircuitConvertor.py", line 23, in init
self.init_intermediate_circuit(circuit)
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/qibotn-0.0.1.dev0-py3.9.egg/qibotn/QiboCircuitConvertor.py", line 102, in init_intermediate_circuit
cp.asarray(gates.FusedGate(gate).matrix, dtype=self.dtype).reshape(
File "/home/liwei/miniconda3/envs/cuquantum_env/lib/python3.9/site-packages/cupy/_creation/from_data.py", line 76, in asarray
return _core.array(a, dtype, False, order)
File "cupy/_core/core.pyx", line 2360, in cupy._core.core.array
File "cupy/_core/core.pyx", line 2384, in cupy._core.core.array
File "cupy/_core/core.pyx", line 2509, in cupy._core.core._array_default
TypeError: must be real number, not method

It seems that the gate.matrix return value is not compatible with cupy.asarray() any more. We need to resolve this, maybe in another PR? @alecandido

A quick fix. Change gate.matrix to gate.matrix() can resolve this compatibility issue.

@liweintu liweintu closed this Sep 27, 2023
@liweintu liweintu reopened this Sep 27, 2023
@alecandido
Copy link
Member

I was about to check, thanks for solving it on your side :)

@Tankya2
Copy link
Contributor Author

Tankya2 commented Oct 12, 2023

Hi @alecandido wondering if there is any other outstanding issues with this PR? Please let me know so that we can move to the next step

Copy link
Member

@alecandido alecandido left a comment

Choose a reason for hiding this comment

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

To me it looks ok.

Sorry if it took so long for the review: I've been a bit busy (and eventually I actually forgot, thanks for the gentle reminder).

I leave some further comments for minor improvements.
About the change suggestions: pay attention (as usual) because not all the suggestions are self-contained, so check before committing them from the web interface.

Comment on lines +11 to +12
mps_tensors = [state_tensor] * num_qubits
return mps_tensors
Copy link
Member

Choose a reason for hiding this comment

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

It's not necessarily bad, but it is just simpler to avoid the assignment, since you're immediately returning (unless you're very motivated to give it a name, but the name will usually be very close to the function one)

Suggested change
mps_tensors = [state_tensor] * num_qubits
return mps_tensors
return [state_tensor] * num_qubits

Generate the MPS with an initial state of |00...00>
"""
state_tensor = cp.asarray([1, 0], dtype=dtype).reshape(1, 2, 1)
mps_tensors = [state_tensor] * num_qubits
Copy link
Member

Choose a reason for hiding this comment

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

I believe CuPy arrays to be immutable, so it should not be a problem.
But try to confirm it, because, otherwise, you'd be generating many references to the same object, that might have counter-intuitive effects (that is even if you modify just one, all the others will change as well.

Comment on lines +7 to +9
"""
Generate the MPS with an initial state of |00...00>
"""
Copy link
Member

Choose a reason for hiding this comment

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

Sooner or later we'll start using Pydocstring, and it will check that every first line will end with a full stop.

Suggested change
"""
Generate the MPS with an initial state of |00...00>
"""
"""Generate the MPS with an initial state of |00...00>."""

(this is just an irrelevant remark, because this PR is already pretty good, so we can improve even some details).


def mps_site_right_swap(mps_tensors, i, **kwargs):
"""
Perform the swap operation between the ith and i+1th MPS tensors.
Copy link
Member

Choose a reason for hiding this comment

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

Tell the user that the operation will be performed in-place.

options=kwargs.get("options", None)
)
mps_tensors[i : i + 2] = (a, b)
return mps_tensors
Copy link
Member

Choose a reason for hiding this comment

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

Since the operation is in-place, you do not need to return the modified object, since it's the one passed by the user.
(and there should be only one way of doing something, so it would be confusing to have the two references around)

Comment on lines +20 to +23
self.num_qubits = circ_qibo.nqubits
self.handle = cutn.create()
self.dtype = dtype
self.mps_tensors = initial(self.num_qubits, dtype=dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need all these assignments to self?

If possible, keep everything local (in this case local to the __init__ method).

(i + offset, 2 * i + 1, i + 1 + offset) for i in range(num_qubits)
]

def contract_norm(self, mps_tensors, options=None):
Copy link
Member

Choose a reason for hiding this comment

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

Just a proposal to simplify the method names: since the class is already named MPSContractionHelper, you could avoid prefixing methods with contract_ (since it is reasonable that an MPSContractionHelper.norm method will make a contraction to give you the norm).

However, it is just a proposal and definitely subject to opinions. So, feel free to keep as it is.

The norm of the MPS.
"""
interleaved_inputs = []
for i, o in enumerate(mps_tensors):
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
for i, o in enumerate(mps_tensors):
for i, op in enumerate(mps_tensors):

To slightly improve the name's expresiveness, at very little expense ^^

An ndarray-like object as the state vector.
"""
interleaved_inputs = []
for i, o in enumerate(mps_tensors):
Copy link
Member

Choose a reason for hiding this comment

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

Same of above

Suggested change
for i, o in enumerate(mps_tensors):
for i, op in enumerate(mps_tensors):

@liweintu liweintu merged commit cb0a6f0 into main Oct 17, 2023
@alecandido alecandido deleted the cuQuantum_mps branch March 1, 2024 22:11
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