Skip to content

Commit

Permalink
Format with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankya2 committed Jan 24, 2024
1 parent 214fdf6 commit 6667caa
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 217 deletions.
85 changes: 43 additions & 42 deletions src/qibotn/QiboCircuitConvertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def init_intermediate_circuit(self, circuit):
required_shape = self.op_shape_from_qubits(len(gate_qubits))
self.gate_tensors.append(
(
cp.asarray(gate.matrix(), dtype=self.dtype).reshape(
required_shape),
cp.asarray(gate.matrix(), dtype=self.dtype).reshape(required_shape),
gate_qubits,
)
)
Expand All @@ -111,7 +110,6 @@ def init_basis_map(self, backend, dtype):

self.basis_map = {"0": state_0, "1": state_1}


def init_inverse_circuit(self, circuit):
self.gate_tensors_inverse = []
gates_qubits_inverse = []
Expand All @@ -132,85 +130,88 @@ def init_inverse_circuit(self, circuit):

# self.active_qubits is to identify qubits with at least 1 gate acting on it in the whole circuit.
self.active_qubits_inverse = np.unique(gates_qubits_inverse)


def get_pauli_gates(self, pauli_map, dtype='complex128', backend=cp):

def get_pauli_gates(self, pauli_map, dtype="complex128", backend=cp):
"""
Populate the gates for all pauli operators.
Args:
pauli_map: A dictionary mapping qubits to pauli operators.
pauli_map: A dictionary mapping qubits to pauli operators.
dtype: Data type for the tensor operands.
backend: The package the tensor operands belong to.
Returns:
A sequence of pauli gates.
"""
asarray = backend.asarray
pauli_i = asarray([[1,0], [0,1]], dtype=dtype)
pauli_x = asarray([[0,1], [1,0]], dtype=dtype)
pauli_y = asarray([[0,-1j], [1j,0]], dtype=dtype)
pauli_z = asarray([[1,0], [0,-1]], dtype=dtype)

operand_map = {'I': pauli_i,
'X': pauli_x,
'Y': pauli_y,
'Z': pauli_z}
pauli_i = asarray([[1, 0], [0, 1]], dtype=dtype)
pauli_x = asarray([[0, 1], [1, 0]], dtype=dtype)
pauli_y = asarray([[0, -1j], [1j, 0]], dtype=dtype)
pauli_z = asarray([[1, 0], [0, -1]], dtype=dtype)

operand_map = {"I": pauli_i, "X": pauli_x, "Y": pauli_y, "Z": pauli_z}
gates = []
for qubit, pauli_char in pauli_map.items():
operand = operand_map.get(pauli_char)
if operand is None:
raise ValueError('pauli string character must be one of I/X/Y/Z')
raise ValueError("pauli string character must be one of I/X/Y/Z")
gates.append((operand, (qubit,)))
return gates

def expectation_operands(self, pauli_string):
#assign pauli string to qubit
#_get_forward_inverse_metadata()
input_bitstring = "0" * self.circuit.nqubits #Need all qubits!
# assign pauli string to qubit
# _get_forward_inverse_metadata()
input_bitstring = "0" * self.circuit.nqubits # Need all qubits!

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

(
mode_labels,
qubits_frontier,
next_frontier,
) = self._init_mode_labels_from_qubits(range(self.circuit.nqubits))

gate_mode_labels, gate_operands = self._parse_gates_to_mode_labels_operands(
self.gate_tensors, qubits_frontier, next_frontier
)

operands = input_operands + gate_operands
mode_labels += gate_mode_labels

self.init_inverse_circuit(self.circuit.invert())



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

#input_mode_labels, input_operands, qubits_frontier, next_frontier, inverse_gates = self._get_forward_inverse_metadata(coned_qubits)
# input_mode_labels, input_operands, qubits_frontier, next_frontier, inverse_gates = self._get_forward_inverse_metadata(coned_qubits)

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

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


gates_inverse = pauli_gates + self.gate_tensors_inverse

gate_mode_labels_inverse, gate_operands_inverse = self._parse_gates_to_mode_labels_operands(

(
gate_mode_labels_inverse,
gate_operands_inverse,
) = self._parse_gates_to_mode_labels_operands(
gates_inverse, qubits_frontier, next_frontier
)
mode_labels = mode_labels + gate_mode_labels_inverse + [[qubits_frontier[ix]] for ix in range(self.circuit.nqubits)]
operands = operands + gate_operands_inverse + operands[:self.circuit.nqubits]

mode_labels = (
mode_labels
+ gate_mode_labels_inverse
+ [[qubits_frontier[ix]] for ix in range(self.circuit.nqubits)]
)
operands = operands + gate_operands_inverse + operands[: self.circuit.nqubits]

operand_exp_interleave = [x for y in zip(operands, mode_labels) for x in y]

#expec = contract(*operand_exp_interleave)
#print(expec)

'''
# expec = contract(*operand_exp_interleave)
# print(expec)

"""
gate_mode_labels, gate_operands = circ_utils.parse_gates_to_mode_labels_operands(gates,
qubits_frontier,
next_frontier)
Expand All @@ -220,5 +221,5 @@ def expectation_operands(self, pauli_string):
output_mode_labels = []
expression = circ_utils.convert_mode_labels_to_expression(mode_labels, output_mode_labels)
'''
return operand_exp_interleave
"""
return operand_exp_interleave
41 changes: 19 additions & 22 deletions src/qibotn/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def __init__(self, platform):
or platform == "cu_tensornet_expectation"
or platform == "cu_tensornet_nccl"
or platform == "cu_tensornet_nccl_expectation"


): # pragma: no cover
self.platform = platform
else:
Expand Down Expand Up @@ -72,56 +70,55 @@ def execute_circuit(
state = cutn.eval_mps(circuit, gate_algo, self.dtype)

if self.platform == "qu_tensornet":

#init_state = np.random.random(2**circuit.nqubits) + 1j * np.random.random(2**circuit.nqubits)
#init_state = init_state / np.sqrt((np.abs(init_state) ** 2).sum())
# init_state = np.random.random(2**circuit.nqubits) + 1j * np.random.random(2**circuit.nqubits)
# init_state = init_state / np.sqrt((np.abs(init_state) ** 2).sum())
init_state = np.zeros(2**circuit.nqubits, dtype=self.dtype)
init_state[0] = 1.0
state = quimb.eval(circuit.to_qasm(), init_state, backend="numpy")

if self.platform == "cu_tensornet_mpi":
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

#state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
state, rank = cutn.eval_tn_MPI_2(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
state, rank = cutn.eval_tn_MPI_2(circuit, self.dtype, 32)
if rank > 0:
state = np.array(0)

if self.platform == "cu_tensornet_nccl":
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

#state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
state, rank = cutn.eval_tn_nccl(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
state, rank = cutn.eval_tn_nccl(circuit, self.dtype, 32)
if rank > 0:
state = np.array(0)

if self.platform == "cu_tensornet_expectation":
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

state = cutn.eval_expectation(circuit, self.dtype)

if self.platform == "cu_tensornet_mpi_expectation":
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

#state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
#state, rank = cutn.eval_tn_MPI_expectation(circuit, self.dtype,32)
state, rank = cutn.eval_tn_MPI_2_expectation(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI_expectation(circuit, self.dtype,32)
state, rank = cutn.eval_tn_MPI_2_expectation(circuit, self.dtype, 32)

if rank > 0:
state = np.array(0)

if self.platform == "cu_tensornet_nccl_expectation":
if initial_state is not None:
raise_error(NotImplementedError, "QiboTN cannot support initial state.")

#state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
#state, rank = cutn.eval_tn_MPI_expectation(circuit, self.dtype,32)
state, rank = cutn.eval_tn_nccl_expectation(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI(circuit, self.dtype,32)
# state, rank = cutn.eval_tn_MPI_expectation(circuit, self.dtype,32)
state, rank = cutn.eval_tn_nccl_expectation(circuit, self.dtype, 32)

if rank > 0:
state = np.array(0)

Expand Down
Loading

0 comments on commit 6667caa

Please sign in to comment.