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

PauliEvolutionGate default label can be too long to serialize via qpy #12117

Open
garrison opened this issue Apr 2, 2024 · 2 comments
Open
Labels
bug Something isn't working mod: qpy Related to QPY serialization

Comments

@garrison
Copy link
Member

garrison commented Apr 2, 2024

Environment

  • Qiskit version: main
  • Python version: 3.11
  • Operating system: Linux

What is happening?

To serialize an EvolvedOperatorAnsatz requires serializing a label containing the entire string representing the SparsePauliOp. For a QAOAAnsatz, which is a subclass of EvolvedOperatorAnsatz, this might look like exp(-it (IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZZ + IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIZ + {...} )), where I have truncated and replaced many terms with {...}.

The problem is that this label can easily exceed the maximum allowed qpy label length of 65536 after encoding (as the label length is represented by a 16-bit unsigned integer).

In terms of code, EvolvedOperatorAnsatz relies on PauliEvolutionGate, which itself creates the label.

How can we reproduce the issue?

Create a QAOAAnsatz with many terms and over many (>100 qubits). Serialize it to qpy.

What should happen?

Somehow, no error should occur. The user should not have to modify or discard the label in order to dump to qpy. (Actually, it's not even straightforward how I'd go about discarding or modifying this label, since in order to do so I'd need to somehow access the PauliEvolutionGate through the EvolvedOperatorAnsatz.)

Any suggestions?

One of the following:

  1. Truncate the label in a graceful way, at the boundary between two terms if the string is getting close to "too long" (which should be < 65536 characters, but possibly much less).
  2. Modify the qpy spec to support longer labels.

I prefer the first option unless there is a strong case for keeping the entire label intact.

@garrison garrison added the bug Something isn't working label Apr 2, 2024
@Lancelot03
Copy link

Lancelot03 commented Apr 3, 2024

hi, @garrison as by your suggestion we can implement the truncate function like this

def serialize(self):
        label = self._get_full_label()
        truncated_label = self._truncate_label(label)
            return truncated_label
    
def _truncate_label(self, label, max_length=65536):
        if len(label) <= max_length:
            return label
        else:
            index = max_length - 1
            while index >= 0 and label[index] != '+':
                index -= 1
            if index < 0:
                raise ValueError("Cannot truncate label without breaking terms")
            return label[:index] + ' {...}'

we can do some modification in the code according to the types of input.

@jakelishman
Copy link
Member

I think there's probably two parts to the solution here:

  • PauliEvolutionGate absolutely should not be producing by default labels that use that many characters. It's not helpful to anybody, and I imagine it's slow to make as well - I agree we should definitely change that to something more sensible, arguably avoiding eagerly serialising the inner SparsePauliOp into a less efficient representation when the gate is created, even if we do partially truncate it - it seems very wasteful and prone to this sort of explosion. I don't use the gate, though, so I don't know what might be more useful.
  • QPY should have an escape mechanism to avoid hard crashing - maybe we just emit a warning on too-long labels and truncate them at the maximum length. We could consider introducing a new QPY format version to support arbitrary-length labels in some form or another too, if it's necessary.

@jakelishman jakelishman added the mod: qpy Related to QPY serialization label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qpy Related to QPY serialization
Projects
None yet
Development

No branches or pull requests

3 participants