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

Support general matrix elements in backend #128

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
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
53 changes: 42 additions & 11 deletions pytket/extensions/cutensornet/backends/cutensornet_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ def process_circuits(
handle_list.append(handle)
return handle_list

# TODO: this should be optionally parallelised with MPI
# (both wrt Pauli strings and contraction itself).
def get_operator_expectation_value(
self,
state_circuit: Circuit,
Expand All @@ -261,13 +259,46 @@ def get_operator_expectation_value(
Returns:
Expectation value.
"""
return self.get_matrix_element(
state_circuit,
state_circuit,
operator,
post_selection=post_selection,
valid_check=valid_check,
)

# TODO: this should be optionally parallelised with MPI
# (both wrt Pauli strings and contraction itself).
def get_matrix_element(
self,
circuit_bra: Circuit,
circuit_ket: Circuit,
operator: QubitPauliOperator,
post_selection: Optional[dict[Qubit, int]] = None,
valid_check: bool = True,
) -> float:
"""Calculates a general matrix element using cuTensorNet contraction.

Has an option to do post selection on an ancilla register.

Args:
circuit_bra: Circuit representing bra state.
circuit_ket: Circuit representing ket state.
operator: Operator which matrix element is to be calculated.
valid_check: Whether to perform circuit validity check.
post_selection: Dictionary of qubits to post select where the key is
qubit and the value is bit outcome.

Returns:
Matrix element.
"""
if valid_check:
self._check_all_circuits([state_circuit])
self._check_all_circuits([circuit_bra, circuit_ket])

expectation = 0
element = 0

ket_network = TensorNetwork(state_circuit)
bra_network = ket_network.dagger()
bra_network = TensorNetwork(circuit_bra)
ket_network = TensorNetwork(circuit_ket).dagger()
obackhouse marked this conversation as resolved.
Show resolved Hide resolved

if post_selection is not None:
post_select_qubits = list(post_selection.keys())
Expand All @@ -281,18 +312,18 @@ def get_operator_expectation_value(
) # This needed because dagger does not work with post selection

for qos, coeff in operator._dict.items():
expectation_value_network = ExpectationValueTensorNetwork(
element_network = ExpectationValueTensorNetwork(
bra_network, qos, ket_network
)
if isinstance(coeff, Expr):
numeric_coeff = complex(coeff.evalf()) # type: ignore
else:
numeric_coeff = complex(coeff) # type: ignore
expectation_term = numeric_coeff * cq.contract(
*expectation_value_network.cuquantum_interleaved
element_term = numeric_coeff * cq.contract(
*element_network.cuquantum_interleaved
)
expectation += expectation_term
return expectation.real
element += element_term
return element.real
obackhouse marked this conversation as resolved.
Show resolved Hide resolved

def get_circuit_overlap(
self,
Expand Down
Loading