qiskit-advocate / qamp-fall-22

Qiskit advocate mentorship program (QAMP) fall 22 cohort (Sep - Dec 2022)
15 stars 7 forks source link

Add good __repr__ methods to all public classes #37

Closed kevinsung closed 2 years ago

kevinsung commented 2 years ago

Description

See https://github.com/Qiskit/qiskit-terra/issues/8594.

Deliverables

Fix or make progress towards fixing https://github.com/Qiskit/qiskit-terra/issues/8594.

Mentors details

Number of mentees

1

Type of mentees

BramDo commented 2 years ago

I am interested in this issue, because it is good way to learn qiskit classes. However it might be lot of work todo all classes

For the class QuantumCircuit I have used the following repr:

#<QuantumCircuit '<name>' with <x> qubits, <y> clbits and <z> instructions>
def __repr__(self):
        return "<QuantumCircuit '%s' with %d qubits, %d clbits and %d instructions>" % (
            self.name,
            len(self.qregs) + 1,
            len(self.cregs) + 1,
            len(self._data),
        )

With testcircuit:

def build_bell_circuit():
    """Returns a circuit putting 2 qubits in the Bell state."""
    qc = QuantumCircuit(2, 2)
    qc.h(0)
    qc.cx(0, 1)
    qc.measure([0, 1], [0, 1])
    return qc

# Create the circuit
bell_circuit = build_bell_circuit()

Output in python shell

repr(bell_circuit) "<QuantumCircuit 'circuit-1' with 2 qubits, 2 clbits and 4 instructions>"

kevinsung commented 2 years ago

Thank you @BramDo ! I would say the first step would be to start making a list of the classes that need attention. I mentioned QuantumCircuit and Sampler at https://github.com/Qiskit/qiskit-terra/issues/8594 but there are probably more. Once we have a list of all the classes, we can sort them by perceived importance and start from there. If you want to start investigating, you can write the list of classes in a comment at https://github.com/Qiskit/qiskit-terra/issues/8594.

BramDo commented 2 years ago

For adding a repr function to sampler class, I started with the qiskit_ibm_runtime as separate module

def __repr__(self):
        return f"{self.__class__.__name__}({self._initial_inputs})"

Output

Sampler({'circuits': <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x7ff8062b9040>, 'parameters': None})

This shows the standard quantumcircuit class. Offcourse we want the new QAMP repr style. It took me some time but I needed to load the modified qiskit-terra and qiskit-ibm-runtime both in the dev environment. Then I have the new quantumcircuit repr in the ibm-runtime repr. Output

Sampler({'circuits': <QuantumCircuit 'circuit-92' with 2 qubits, 2 clbits and 5 instructions>, 'parameters': None})
BramDo commented 2 years ago

Update of read_from instructions method as part of the QuantumCircuit class

    @classmethod
    def from_instructions(cls, instructions: Iterable[CircuitInstruction], *, name=None, metadata=None, global_phase=0):
        out = cls(name=name, metadata=metadata, global_phase=global_phase)
        qc = QuantumCircuit(2,2)
        for instruction in instructions:
            qc._append(instruction)
        return qc

I made a testcircuit

from qiskit import QuantumCircuit
circuit = QuantumCircuit()

def build_bell_circuit():
    qc = QuantumCircuit(2,2)
    qc.h(0)
    qc.cx(0,1)
    return qc

# Read instructions from the circuit
print(circuit.from_instructions(build_bell_circuit()))
GemmaDawson commented 2 years ago

Unfortunately, this project will not be going forward.