Skip to content

Commit

Permalink
Update to allow user to set MPS parameters and to set Pauli string pa…
Browse files Browse the repository at this point in the history
…ttern
  • Loading branch information
Tankya2 committed Jan 31, 2024
1 parent 9b8058d commit a3f3538
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
60 changes: 34 additions & 26 deletions src/qibotn/backends/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,43 @@ def __init__(self, runcard):
super().__init__()
import cuquantum # pylint: disable=import-error
from cuquantum import cutensornet as cutn # pylint: disable=import-error

self.pauli_string_pattern = "XXXZ"

if runcard is not None:
self.MPI_enabled = runcard.get("MPI_enabled", False)
self.MPS_enabled = runcard.get("MPS_enabled", False)
self.NCCL_enabled = runcard.get("NCCL_enabled", False)

expectation_enabled_value = runcard.get('expectation_enabled')

expectation_enabled_value = runcard.get("expectation_enabled")
if expectation_enabled_value is True:
self.expectation_enabled = True

print("expectation_enabled is",self.expectation_enabled)
self.pauli_string_pattern = "XXXZ"
elif expectation_enabled_value is False:
self.expectation_enabled = False

print("expectation_enabled is",self.expectation_enabled)
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)

print("expectation_enabled is a dictionary",self.expectation_enabled,self.pauli_string_pattern )
expectation_enabled_dict = runcard.get("expectation_enabled", {})
self.pauli_string_pattern = expectation_enabled_dict.get(
"pauli_string_pattern", None
)
else:
raise TypeError("expectation_enabled has an unexpected type")


mps_enabled_value = runcard.get("MPS_enabled")
if mps_enabled_value is True:
self.MPS_enabled = True
self.gate_algo = {
"qr_method": False,
"svd_method": {
"partition": "UV",
"abs_cutoff": 1e-12,
},
}
elif mps_enabled_value is False:
self.MPS_enabled = False
elif isinstance(mps_enabled_value, dict):
self.MPS_enabled = True
self.gate_algo = runcard.get("MPS_enabled", {})
else:
raise TypeError("MPS_enabled has an unexpected type")

else:
self.MPI_enabled = False
Expand Down Expand Up @@ -123,14 +132,7 @@ def execute_circuit(
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

gate_algo = {
"qr_method": False,
"svd_method": {
"partition": "UV",
"abs_cutoff": 1e-12,
},
} # make this user input
state = eval.dense_vector_mps(circuit, gate_algo, self.dtype)
state = eval.dense_vector_mps(circuit, self.gate_algo, self.dtype)

elif (
self.MPI_enabled == True
Expand Down Expand Up @@ -167,7 +169,9 @@ def execute_circuit(
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

state = eval.expectation_pauli_tn(circuit, self.dtype, self.pauli_string_pattern)
state = eval.expectation_pauli_tn(
circuit, self.dtype, self.pauli_string_pattern
)

elif (
self.MPI_enabled == True
Expand All @@ -178,7 +182,9 @@ def execute_circuit(
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

state, rank = eval.expectation_pauli_tn_MPI(circuit, self.dtype, self.pauli_string_pattern, 32)
state, rank = eval.expectation_pauli_tn_MPI(
circuit, self.dtype, self.pauli_string_pattern, 32
)

if rank > 0:
state = np.array(0)
Expand All @@ -192,7 +198,9 @@ def execute_circuit(
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

state, rank = eval.expectation_pauli_tn_nccl(circuit, self.dtype, self.pauli_string_pattern, 32)
state, rank = eval.expectation_pauli_tn_nccl(
circuit, self.dtype, self.pauli_string_pattern, 32
)

if rank > 0:
state = np.array(0)
Expand Down
14 changes: 10 additions & 4 deletions src/qibotn/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def dense_vector_tn(qibo_circ, datatype):
def expectation_pauli_tn(qibo_circ, datatype, pauli_string):
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
return contract(
*myconvertor.expectation_operands(PauliStringGen(qibo_circ.nqubits, pauli_string))
*myconvertor.expectation_operands(
PauliStringGen(qibo_circ.nqubits, pauli_string)
)
)


Expand Down Expand Up @@ -231,7 +233,9 @@ def expectation_pauli_tn_nccl(qibo_circ, datatype, pauli_string, n_samples=8):
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
# mem_avail = cp.cuda.Device().mem_info[0]
# print("Mem avail: aft convetor",mem_avail, "rank =",rank)
operands = myconvertor.expectation_operands(PauliStringGen(qibo_circ.nqubits, pauli_string))
operands = myconvertor.expectation_operands(
PauliStringGen(qibo_circ.nqubits, pauli_string)
)

# mem_avail = cp.cuda.Device().mem_info[0]
# print("Mem avail: aft operand interleave",mem_avail, "rank =",rank)
Expand Down Expand Up @@ -310,7 +314,9 @@ def expectation_pauli_tn_MPI(qibo_circ, datatype, pauli_string, n_samples=8):
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
# mem_avail = cp.cuda.Device().mem_info[0]
# print("Mem avail: aft convetor",mem_avail, "rank =",rank)
operands = myconvertor.expectation_operands(PauliStringGen(qibo_circ.nqubits, pauli_string))
operands = myconvertor.expectation_operands(
PauliStringGen(qibo_circ.nqubits, pauli_string)
)
# mem_avail = cp.cuda.Device().mem_info[0]
# print("Mem avail: aft operand interleave",mem_avail, "rank =",rank)

Expand Down Expand Up @@ -383,7 +389,7 @@ def PauliStringGen(nqubits, pauli_string):
return "Invalid input. N should be a positive integer."

characters = pauli_string
#characters = "XXXZ"
# characters = "XXXZ"

result = ""

Expand Down

0 comments on commit a3f3538

Please sign in to comment.