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

Update tn #74

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
17813a6
Fix bug
Tankya2 Jul 4, 2024
9d37020
Add configuration and free memory explicitly
Tankya2 Jul 4, 2024
bbacc26
correct missing mempool initialization
Tankya2 Jul 4, 2024
f358d0e
Update NCCL
Tankya2 Jul 29, 2024
1aaa838
Update dense_vector_tn_MPI
Tankya2 Oct 4, 2024
fb5e0e7
Update dense vector tn nccl
Tankya2 Oct 4, 2024
c1dd326
[pre-commit.ci] pre-commit autoupdate
pre-commit-ci[bot] Jul 29, 2024
f4d00c4
[pre-commit.ci] pre-commit autoupdate
pre-commit-ci[bot] Aug 5, 2024
8aef3af
Comment
Tankya2 Oct 4, 2024
fe83631
Format
Tankya2 Oct 4, 2024
c3a845c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2024
f33c44e
Merge branch 'main' into update_tn
Tankya2 Oct 25, 2024
f9e74fe
Remove import quantum
Tankya2 Oct 30, 2024
289c8e2
Remove duplication
Tankya2 Oct 30, 2024
191949d
Remove the unnecessary deletion because automatic deletion is implici…
Nov 20, 2024
27b378b
Remove redundant mem pool
Tankya2 Dec 18, 2024
8dd1ea5
Merge branch 'main' into update_tn
Tankya2 Feb 13, 2025
2aa208f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 13, 2025
e25c08a
Refactor to reduce repeating codes
Tankya2 Feb 18, 2025
7fce1d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 18, 2025
63480af
Add test for expectation and updates
Tankya2 Feb 20, 2025
d4d18ec
Change to expectation calculation to accept hamiltonian object
Tankya2 Feb 20, 2025
f4a01a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions src/qibotn/backends/cutensornet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import numpy as np
from qibo import hamiltonians
from qibo.backends import NumpyBackend
from qibo.config import raise_error
from qibo.result import QuantumState

from qibotn.backends.abstract import QibotnBackend
from qibotn.result import TensorNetworkResult

CUDA_TYPES = {}

Expand All @@ -14,6 +15,11 @@ class CuTensorNet(QibotnBackend, NumpyBackend): # pragma: no cover

def __init__(self, runcard):
super().__init__()
from cuquantum import ( # pylint: disable=import-error
ComputeType,
__version__,
cudaDataType,
)
from cuquantum import cutensornet as cutn # pylint: disable=import-error

if runcard is not None:
Expand All @@ -23,15 +29,17 @@ def __init__(self, runcard):
expectation_enabled_value = runcard.get("expectation_enabled")
if expectation_enabled_value is True:
self.expectation_enabled = True
self.pauli_string_pattern = "XXXZ"
self.observable = None
elif expectation_enabled_value is False:
self.expectation_enabled = False
elif isinstance(expectation_enabled_value, dict):
self.expectation_enabled = True
expectation_enabled_dict = runcard.get("expectation_enabled", {})
self.pauli_string_pattern = expectation_enabled_dict.get(
"pauli_string_pattern", None
)
self.observable = runcard.get("expectation_enabled", {})
elif isinstance(
expectation_enabled_value, hamiltonians.SymbolicHamiltonian
):
self.expectation_enabled = True
self.observable = expectation_enabled_value
else:
raise TypeError("expectation_enabled has an unexpected type")

Expand Down Expand Up @@ -60,22 +68,21 @@ def __init__(self, runcard):
self.expectation_enabled = False

self.name = "qibotn"
self.cuquantum = cuquantum
self.cutn = cutn
self.platform = "cutensornet"
self.versions["cuquantum"] = self.cuquantum.__version__
self.versions["cuquantum"] = __version__
self.supports_multigpu = True
self.handle = self.cutn.create()

global CUDA_TYPES
CUDA_TYPES = {
"complex64": (
self.cuquantum.cudaDataType.CUDA_C_32F,
self.cuquantum.ComputeType.COMPUTE_32F,
cudaDataType.CUDA_C_32F,
ComputeType.COMPUTE_32F,
),
"complex128": (
self.cuquantum.cudaDataType.CUDA_C_64F,
self.cuquantum.ComputeType.COMPUTE_64F,
cudaDataType.CUDA_C_64F,
ComputeType.COMPUTE_64F,
),
}

Expand Down Expand Up @@ -154,17 +161,15 @@ def execute_circuit(
and self.NCCL_enabled == False
and self.expectation_enabled == True
):
state = eval.expectation_pauli_tn(
circuit, self.dtype, self.pauli_string_pattern
)
state = eval.expectation_tn(circuit, self.dtype, self.observable)
elif (
self.MPI_enabled == True
and self.MPS_enabled == False
and self.NCCL_enabled == False
and self.expectation_enabled == True
):
state, rank = eval.expectation_pauli_tn_MPI(
circuit, self.dtype, self.pauli_string_pattern, 32
state, rank = eval.expectation_tn_MPI(
circuit, self.dtype, self.observable, 32
)
if rank > 0:
state = np.array(0)
Expand All @@ -174,15 +179,27 @@ def execute_circuit(
and self.NCCL_enabled == True
and self.expectation_enabled == True
):
state, rank = eval.expectation_pauli_tn_nccl(
circuit, self.dtype, self.pauli_string_pattern, 32
state, rank = eval.expectation_tn_nccl(
circuit, self.dtype, self.observable, 32
)
if rank > 0:
state = np.array(0)
else:
raise_error(NotImplementedError, "Compute type not supported.")

if return_array:
return state.flatten()
if self.expectation_enabled:
return state.flatten().real
else:
return QuantumState(state.flatten())
if return_array:
statevector = state.flatten()
else:
statevector = state

return TensorNetworkResult(
nqubits=circuit.nqubits,
backend=self,
measures=None,
measured_probabilities=None,
prob_type=None,
statevector=statevector,
)
12 changes: 3 additions & 9 deletions src/qibotn/circuit_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,19 @@ def get_pauli_gates(self, pauli_map, dtype="complex128", backend=cp):
gates.append((operand, (qubit,)))
return gates

def expectation_operands(self, pauli_string):
def expectation_operands(self, ham_gates):
"""Create the operands for pauli string expectation computation in the
interleave format.

Parameters:
pauli_string: A string representating the list of pauli gates.
ham_gates: A list of gates derived from Qibo hamiltonian object.

Returns:
Operands for the contraction in the interleave format.
"""
input_bitstring = "0" * self.circuit.nqubits

input_operands = self._get_bitstring_tensors(input_bitstring)
pauli_string = dict(zip(range(self.circuit.nqubits), pauli_string))
pauli_map = pauli_string

(
mode_labels,
Expand All @@ -228,11 +226,7 @@ def expectation_operands(self, pauli_string):

next_frontier = max(qubits_frontier.values()) + 1

pauli_gates = self.get_pauli_gates(
pauli_map, dtype=self.dtype, backend=self.backend
)

gates_inverse = pauli_gates + self.gate_tensors_inverse
gates_inverse = ham_gates + self.gate_tensors_inverse

(
gate_mode_labels_inverse,
Expand Down
Loading