Added MPS codes
This commit is contained in:
@@ -5,68 +5,32 @@ import cuquantum
|
||||
from cuquantum import cutensornet as cutn
|
||||
import cupy as cp
|
||||
import numpy as np
|
||||
from qibo.models import QFT
|
||||
from QiboCircuitToMPS import QiboCircuitToMPS
|
||||
from MPSContractionHelper import MPSContractionHelper
|
||||
|
||||
|
||||
def eval(qibo_circ, datatype):
|
||||
myconvertor = QiboCircuitToEinsum(qibo_circ, dtype=datatype)
|
||||
return contract(*myconvertor.state_vector_operands())
|
||||
|
||||
def eval_mps(qibo_circ, datatype):
|
||||
#Create MPS
|
||||
cutensornet.create()
|
||||
return contract()
|
||||
|
||||
def eval_mps(qibo_circ, gate_algo, datatype):
|
||||
myconvertor = QiboCircuitToMPS(qibo_circ, gate_algo, datatype)
|
||||
mps_helper = MPSContractionHelper(myconvertor.num_qubits)
|
||||
sv_mps = mps_helper.contract_state_vector(myconvertor.mps_tensors,myconvertor.options)
|
||||
return sv_mps
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("cuTensorNet-vers:", cutn.get_version())
|
||||
dev = cp.cuda.Device() # get current device
|
||||
props = cp.cuda.runtime.getDeviceProperties(dev.id)
|
||||
print("===== device info ======")
|
||||
print("GPU-name:", props["name"].decode())
|
||||
print("GPU-clock:", props["clockRate"])
|
||||
print("GPU-memoryClock:", props["memoryClockRate"])
|
||||
print("GPU-nSM:", props["multiProcessorCount"])
|
||||
print("GPU-major:", props["major"])
|
||||
print("GPU-minor:", props["minor"])
|
||||
print("========================")
|
||||
|
||||
data_type = cuquantum.cudaDataType.CUDA_C_64F
|
||||
compute_type = cuquantum.ComputeType.COMPUTE_64F
|
||||
num_sites = 16
|
||||
phys_extent = 2
|
||||
max_virtual_extent = 12
|
||||
|
||||
## we initialize the MPS state as a product state |000...000>
|
||||
initial_state = []
|
||||
for i in range(num_sites):
|
||||
# we create dummpy indices for MPS tensors on the boundary for easier bookkeeping
|
||||
# we'll use Fortran layout throughout this example
|
||||
tensor = cp.zeros((1,2,1), dtype=np.complex128, order="F")
|
||||
tensor[0,0,0] = 1.0
|
||||
initial_state.append(tensor)
|
||||
|
||||
mps_helper = MPSHelper(num_sites, phys_extent, max_virtual_extent, initial_state, data_type, compute_type)
|
||||
|
||||
##################################
|
||||
# Setup options for gate operation
|
||||
##################################
|
||||
|
||||
abs_cutoff = 1e-2
|
||||
rel_cutoff = 1e-2
|
||||
renorm = cutn.TensorSVDNormalization.L2
|
||||
partition = cutn.TensorSVDPartition.UV_EQUAL
|
||||
mps_helper.set_svd_config(abs_cutoff, rel_cutoff, renorm, partition)
|
||||
|
||||
gate_algo = cutn.GateSplitAlgo.REDUCED
|
||||
mps_helper.set_gate_algorithm(gate_algo)
|
||||
|
||||
#####################################
|
||||
# Workspace estimation and allocation
|
||||
#####################################
|
||||
|
||||
free_mem, total_mem = dev.mem_info
|
||||
worksize = free_mem *.7
|
||||
required_workspace_size = mps_helper.compute_max_workspace_sizes()
|
||||
work = cp.cuda.alloc(worksize)
|
||||
print(f"Maximal workspace size requried: {required_workspace_size / 1024 ** 3:.3f} GB")
|
||||
mps_helper.set_workspace(work, required_workspace_size)
|
||||
num_qubits = 25
|
||||
swaps = True
|
||||
circ_qibo = QFT(num_qubits, swaps)
|
||||
|
||||
exact_gate_algorithm = {'qr_method': False,
|
||||
'svd_method':{'partition': 'UV', 'abs_cutoff':1e-12}}
|
||||
dtype="complex128"
|
||||
sv_mps = eval_mps(circ_qibo, exact_gate_algorithm, dtype)
|
||||
sv_reference = eval(circ_qibo, dtype)
|
||||
state_vec = np.array(circ_qibo())
|
||||
print(f"State vector difference: {abs(sv_mps-sv_reference).max():0.3e}")
|
||||
assert cp.allclose(sv_mps, sv_reference)
|
||||
assert cp.allclose(sv_mps.flatten(), state_vec)
|
||||
|
||||
Reference in New Issue
Block a user