Skip to content

Commit

Permalink
integrated the change of basis method into the measurement method
Browse files Browse the repository at this point in the history
  • Loading branch information
positr0nium committed Nov 17, 2024
1 parent e686509 commit ee2dbfc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/qrisp/operators/qubit/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_measurement(
compilation_kwargs={},
subs_dic={},
precompiled_qc=None,
diagonalisation_method="ommuting_qw",
measurement_data=None # measurement settings
):
r"""
Expand Down Expand Up @@ -137,34 +138,37 @@ def get_measurement(
qc = qc.transpile()

if measurement_data is None:
measurement_data = QubitOperatorMeasurement(hamiltonian)
measurement_data = QubitOperatorMeasurement(hamiltonian, diagonalisation_method = diagonalisation_method)

return measurement_data.get_measurement(qc, qarg, precision, backend)


class QubitOperatorMeasurement:

def __init__(self, qubit_op):
def __init__(self, qubit_op, diagonalisation_method="commuting_qw"):

n = qubit_op.find_minimal_qubit_amount()
hamiltonian = qubit_op.hermitize()
hamiltonian = hamiltonian.eliminate_ladder_conjugates()

self.groups = hamiltonian.commuting_qw_groups()
if diagonalisation_method=="commuting_qw":
self.groups = hamiltonian.commuting_qw_groups()
else:
self.groups = hamiltonian.group_up(lambda a, b: a.commute(b))
# print(len(self.groups))

self.n = len(self.groups)


self.stds = []
self.conjugation_circuits = []
self.change_of_basis_gates = []
self.measurement_operators = []

for group in self.groups:

conj_circuit, meas_op = group.get_conjugation_circuit()
qv = QuantumVariable(group.find_minimal_qubit_amount())

self.conjugation_circuits.append(conj_circuit)
meas_op = group.change_of_basis(qv, diagonalisation_method)
self.change_of_basis_gates.append(qv.qs.to_gate())
self.measurement_operators.append(meas_op)

# Collect standard deviation
Expand All @@ -183,14 +187,15 @@ def get_measurement(self, qc, qubit_list, precision, backend):
for i in range(self.n):

group = self.groups[i]
conjugation_circuit = self.conjugation_circuits[i]

shots = int(self.shots_list[i]/precision**2)

qubits = [qubit_list[j] for j in range(self.change_of_basis_gates[i].num_qubits)]

curr = qc.copy()
qubits = [qubit_list[i] for i in range(conjugation_circuit.num_qubits())]
curr.append(conjugation_circuit.to_gate(), qubits)
curr.append(self.change_of_basis_gates[i], qubits)

res = get_measurement_from_qc(curr.transpile(), list(qubit_list), backend, shots)
res = get_measurement_from_qc(curr, list(qubit_list), backend, shots)
results.append(res)

temp_meas_ops = []
Expand All @@ -201,7 +206,7 @@ def get_measurement(self, qc, qubit_list, precision, backend):

meas_coeffs.append(temp_coeff)
meas_ops.append(temp_meas_ops)

samples = create_padded_array([list(res.keys()) for res in results]).astype(np.int64)
probs = create_padded_array([list(res.values()) for res in results])
meas_ops = create_padded_array(meas_ops, use_tuples = True).astype(np.int64)
Expand Down
2 changes: 2 additions & 0 deletions src/qrisp/operators/qubit/qubit_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ def get_measurement(
compilation_kwargs={},
subs_dic={},
precompiled_qc=None,
diagonalisation_method="commuting_qw",
measurement_data=None # measurement settings
):
r"""
Expand Down Expand Up @@ -1342,6 +1343,7 @@ def get_measurement(
compilation_kwargs=compilation_kwargs,
subs_dic=subs_dic,
precompiled_qc=precompiled_qc,
diagonalisation_method=diagonalisation_method,
measurement_data=measurement_data)

#
Expand Down
3 changes: 2 additions & 1 deletion tests/operator_tests/test_measurement_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def testing_helper(qv):
continue

print(H)
assert abs(H.get_measurement(qv, precision = 0.001, shots = int(1E8)) - H.to_pauli().get_measurement(qv, precision = 0.001, shots = int(1E8))) < 1E-2
assert abs(H.get_measurement(qv, precision = 0.0005, shots = int(1E8)) - H.to_pauli().get_measurement(qv, precision = 0.0005, shots = int(1E8))) < 1E-2
assert abs(H.get_measurement(qv, precision = 0.0005, shots = int(1E8), diagonalisation_method = "commuting") - H.to_pauli().get_measurement(qv, diagonalisation_method="commuting", precision = 0.0005, shots = int(1E8))) < 1E-2

qv = QuantumVariable(4)

Expand Down

0 comments on commit ee2dbfc

Please sign in to comment.