Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.31k stars 2.38k forks source link

Add x and y measurements #3967

Open quantumjim opened 4 years ago

quantumjim commented 4 years ago

What is the expected behavior?

Qiskit natively supports z measurements using measure, but x and y basis measurements need to be done with a quick hack (reflecting how the hardware works).

However:

I'd suggest measure_x and measure_y be added to QuantumCircuit, with measure_x compiling down to h followed by measure (except for relevant hardware).

chriseclectic commented 4 years ago

You should just be able to define a composite instruction for these. Eg

class MeasureX(Instruction):

    def __init__(self):
        super().__init__("measure_x", 1, 1, [])

    def broadcast_arguments(self, qargs, cargs):
         # copy from measure instruction

    def _define(self):
        from qiskit.extensions.standard.h import HGate
        from qiskit.circuit.measure import Measure

        q = QuantumRegister(1, 'q')
        c = ClassicalRegister(1, 'c')

        self.definition = [
            (HGate(), [q[0]], []),
            (Measure(), [q[0]], [c[0]])
            (HGate(), [q[0]], []),
        ]

class MeasureY(Instruction):

    def __init__(self):
        super().__init__("measure_y", 1, 1, [])

    def broadcast_arguments(self, qargs, cargs):
         # copy from measure instruction

    def _define(self):
        from qiskit.extensions.standard.h import HGate
        from qiskit.extensions.standard.s import SdgGate
        from qiskit.circuit.measure import Measure

        q = QuantumRegister(1, 'q')
        c = ClassicalRegister(1, 'c')

        self.definition = [
            (SdgGate(), [q[0]], []),
            (HGate(), [q[0]], []),
            (Measure(), [q[0]], [c[0]]),
            (HGate(), [q[0]], []),
            (SdgGate(), [q[0]], [])
        ]

Edited to include post measurement instruction to rotate to the correct collapsed state.

Nisha-Banerjee commented 2 years ago

Update for gosts_of_github_past

This issue may be solved by PR #5311 which is pending for review.

jakelishman commented 11 months ago

Update on this issue, since I've just closed another as a duplicate of it:

We remain interested in generalising the concept of measures that Qiskit can reason about, but it is not at all a trivial task nor a case of just adding a couple of new classes; there are many assumptions baked into the library in many places that measurements are Z-basis, and modifying that will have a lot of knock-on effects throughout the library.

For more context of other discussions that have happened since, see: