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

qpy does not support Discriminator and Kernel #10050

Open
wshanks opened this issue Apr 29, 2023 · 5 comments
Open

qpy does not support Discriminator and Kernel #10050

wshanks opened this issue Apr 29, 2023 · 5 comments
Assignees
Labels
bug Something isn't working mod: pulse Related to the Pulse module mod: qpy Related to QPY serialization

Comments

@wshanks
Copy link
Contributor

wshanks commented Apr 29, 2023

Environment

  • Qiskit Terra version: c4f1b0b
  • Python version: 3.10
  • Operating system: Fedora Linux 37

What is happening?

Trying to serialize with qpy a circuit with a pulse gate which includes an Acquire instruction with a Discriminator or Kernel fails. The error is:

QpyError: "Object type '<class 'qiskit.pulse.configuration.Discriminator'>' is not supported in Value namespace."

and similar for Kernel.

Note this means that with qiskit-ibm-provider pulse gates with discriminators or kernels are not currently supported since circuits must be serialized with qpy.

How can we reproduce the issue?

Here are reproducers for Discriminator:

from io import BytesIO

import numpy as np

from qiskit import QuantumCircuit, pulse
from qiskit.qpy import dump


qc = QuantumCircuit(1, 1)
qc.measure(0, 0)

discriminator = pulse.Discriminator("hw_centroid")

with pulse.builder.build() as sched:
    pulse.builder.acquire(10, 0, pulse.MemorySlot(0), discriminator=discriminator
    
qc.add_calibration("measure", (0,), sched)
    
file_ = BytesIO()
dump(qc, file_)

and Kernel:

from io import BytesIO

import numpy as np

from qiskit import QuantumCircuit, pulse
from qiskit.qpy import dump


qc = QuantumCircuit(1, 1)
qc.measure(0, 0)

kernel = pulse.Kernel("hw_qmfk", kernel={"real": np.zeros(10), "imag": np.zeros(10)}, bias=[0, 0])

with pulse.builder.build() as sched:
    pulse.builder.acquire(10, 0, pulse.MemorySlot(0), kernel=kernel)
    
qc.add_calibration("measure", (0,), sched)
    
file_ = BytesIO()
dump(qc, file_)

What should happen?

Discriminator and Kernel should be serializable with qpy. Since they are part of the circuit, they are already JSON serializable to support the old qobj path.

Any suggestions?

No response

@wshanks wshanks added bug Something isn't working mod: pulse Related to the Pulse module mod: qpy Related to QPY serialization labels Apr 29, 2023
@nkanazawa1989
Copy link
Contributor

Thanks Will. As we discussed offline, we tend to remove them from acquire instruction arguments unless we receive user request.

https://github.com/Qiskit/qiskit-terra/blob/ff80b61fef132930e87d63732d730f5eb5f2ae74/qiskit/qpy/type_keys.py#L322-L324

Keeping them in the acquire instruction indicates we can write a single quantum circuit including different kernels and discriminators, e.g. mid-circuit measurement, which is likely unrealistic. Instead of adding QPY support, we should deprecate them and define alternative path to describe kernel and descriminator.

In OpenPulse grammar, capture instruction is sort of arbitrary, but they show nice example of defining descriminator.

defcalgrammar "openpulse";

cal {
    // Use a boxcar function to generate IQ data from raw waveform
    extern boxcar(waveform input) -> complex[float[64]];
    // Use a linear discriminator to generate bits from IQ data
    extern discriminate(complex[float[64]] iq) -> bit;
    ...
}

defcal measure $0 -> bit {
    ...
    waveform raw_output = capture_v1(capture_frame, 16000dt);

    // Kernel and discriminate
    complex[float[32]] iq = boxcar(raw_output);
    bit result = discriminate(iq);

    return result;
}

https://openqasm.com/language/openpulse.html#capture-instruction

@junnaka51
Copy link
Contributor

@wshanks @nkanazawa1989
Hello 👋 If currently no one is working on this issue, may I try it?

@nkanazawa1989
Copy link
Contributor

Yes, please go ahead! I talked again with @wshanks , and decided to add QPY support of kernel and discriminator rather than dropping them from the acquire instruction. Do you think you can still handle this issue? Feel free to reach me out if you need any help :)

I implemented QPY support of another instruction with this PR. Just FYI
#9890

@junnaka51
Copy link
Contributor

junnaka51 commented Jun 15, 2023

@nkanazawa1989 Thank you so much! 🙇

Do you think you can still handle this issue?

I will see in more detail if I can do so 😄

@junnaka51
Copy link
Contributor

@nkanazawa1989
Hello 👋
The problem seems resolved from the observation that the error no longer raises in #10327

I would like to know whether this approach of mine appropriately follows the convention and/or the rule of qpy or not, before I update docs and add the release note. Could you please briefly check it and give me comments back? 🙏
Thank you so much in advance! 🙇

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: pulse Related to the Pulse module mod: qpy Related to QPY serialization
Projects
None yet
Development

No branches or pull requests

3 participants