Skip to content

Feat adjust measure#198

Open
MathieuG-Colibri wants to merge 12 commits into
devfrom
feat-adjust-measure
Open

Feat adjust measure#198
MathieuG-Colibri wants to merge 12 commits into
devfrom
feat-adjust-measure

Conversation

@MathieuG-Colibri
Copy link
Copy Markdown
Collaborator

@MathieuG-Colibri MathieuG-Colibri commented May 17, 2026

Updated the way to treat expectation measures.

If the measure doesn't cover the whole registry we pad the pauli string with Identities, or is not ordered (instead of swap gates reorganize the matrix or pauli string).

Isn't necessary for braket with optimize_measure set at true (does the pauli grouping).

@MathieuG-Colibri MathieuG-Colibri self-assigned this May 17, 2026
@github-actions github-actions Bot added the enhancement New feature or request label May 17, 2026
@MathieuG-Colibri MathieuG-Colibri changed the base branch from main to dev May 18, 2026 13:17
Comment thread mpqp/execution/providers/ibm.py Outdated
Comment on lines +108 to +111
qiskit_observables.append(translated)
qiskit_observables = [
obs.apply_layout(ibm_circuit.layout) for obs in qiskit_observables
]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do this in a single step ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was deadcode, now deleted e8b7d91

Comment thread mpqp/execution/runner.py Outdated
Comment thread mpqp/tools/circuit.py Outdated
from mpqp.gates import H

for i in range(nb_qubits):
qcircuit.add(H(i))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe random_gate instead of H ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done e8b7d91

Comment thread mpqp/tools/maths.py Outdated
Comment on lines +502 to +515
Example:
>>> I = np.eye(2)
>>> X = np.array([[0,1], [1,0]])
>>> matrix = np.kron(I, X)
>>> pprint(matrix)
[[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]]
>>> pprint(rearrange_matrix(matrix, [1,0]))
[[0, 0, 1, 0],
[0, 0, 0, 1],
[1, 0, 0, 0],
[0, 1, 0, 0]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starting with a diagonal matrix might be more pedagogical/easier to understand what happens

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done e8b7d91

Comment on lines 480 to 485
@@ -484,47 +481,8 @@ def _check_targets_order(self):
"""Ensures target qubits are ordered and contiguous, rearranging them if
necessary (private)."""

if len(self.targets) == 0:
self._pre_measure: list[Gate] = []
return

if self.nb_qubits != self.observables[0].nb_qubits:
raise NumberQubitsError(
f"Target size {self.nb_qubits} doesn't match observable size "
f"{self.observables[0].nb_qubits}."
)

self.rearranged_targets = list(self.targets)
self._pre_measure: list[Gate] = []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this function still needed ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 59fd820
Removed rearranged_targets, it was only used by the old swap path. Also removed pre_measure since Measure.pre_measure already returns [ ]

Comment thread mpqp/execution/providers/aws.py Outdated
]
for circuit in pre_measure:
for instr in circuit.instructions:
instr.targets[0] = job.measure.targets[instr.targets[0]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you changing just the first target ? the logic looks a bit dodgy here

Copy link
Copy Markdown
Collaborator

@MoHermes MoHermes May 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, instr.targets[0] was originally used because find_qubitwise_rotations(...) return single qubit instructions., so each generated instruction has one target. But yeah, this assumption is not obvious, so I agree it looks fragile

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 9ddb86b

f"{self.observables[0].nb_qubits}."
)

self.rearranged_targets = list(self.targets)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the idea here is than now you support arbitrary order, so all the logic
that was here before is not needed anymore, can you confirm ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I confirm your suspicions.

assert isinstance(result, Result)
from mpqp.tools import pprint

pprint(result.amplitudes)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you leave a print?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here 6b12bcc

Comment thread mpqp/execution/providers/aws.py Outdated
]
for circuit in pre_measure:
for instr in circuit.instructions:
instr.targets[0] = job.measure.targets[instr.targets[0]]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why targets[0] ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 9ddb86b

Comment thread mpqp/tools/maths.py
Comment on lines +564 to +613
def rearrange_pauli_string(
ps: PauliString, targets: list[int], copy: bool = True
) -> PauliString:
"""
This function aims at reorderring a pauli string's monomials according to unordered targets.

Note: The targets must be contiguous, otherwise the algorithm won't work.
Args:
ps: The PauliString to reorder.
targets: The list of unordered targets.
copy: If set at True will deepcopy the initial string ps.

Examples:
>>> ps = pX @ pI
>>> print(rearrange_pauli_string(ps, [1,0]))
pI@pX
>>> ps2 = pX @ pI + pI @ pX
>>> print(rearrange_pauli_string(ps2, [1,0]))
pI@pX + pX@pI
"""
if copy:
from copy import deepcopy

pauli = deepcopy(ps)
else:
pauli = ps

l = len(targets)
shuffled = sorted(targets)
for index in range(l):
if targets[index] == index:
continue
shuffled_index = shuffled.index(targets[index])
for monom in pauli.monomials:
atoms = monom.atoms
atoms[shuffled_index], atoms[shuffled[index]] = (
atoms[shuffled[index]],
atoms[shuffled_index],
)
shuffled[index], shuffled[targets[index]] = (
shuffled[targets[index]],
shuffled[index],
)

i = targets.index(index)
targets[i], targets[index] = (
targets[index],
targets[i],
)
return pauli
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to be a method of PauliString

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 01a2724

Comment thread mpqp/tools/maths.py
pauli = ps

l = len(targets)
shuffled = sorted(targets)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"shuffled" means "mélangé", "rearranged" or "reordered" might be clearer

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 01a2724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants