quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.22k stars 1.01k forks source link

Provide a way to iterate through the edges of a device #3696

Closed balopat closed 3 years ago

balopat commented 3 years ago

Is your feature request related to a use case or problem? Please describe.

I am working on the 2 qubit interaction visualization and wanted to generate random characterization data for each of the edges of the Sycamore chip.

Describe the solution you'd like

There is no way to retrieve a list of the edges on the chip - despite the cirq.google.Sycamore representation printing out the diagram nicely.

Describe alternatives/workarounds you've considered

As currently I'm only working on an example, I will just rely on a copy-pasta from the SerializableDevice.__str__ function:

            # Find pairs that are connected by two-qubit gates.
            Pair = Tuple['cirq.GridQubit', 'cirq.GridQubit']
            pairs = {
                cast(Pair, pair)
                for gate_defs in self.gate_definitions.values()
                for gate_def in gate_defs
                if gate_def.number_of_qubits == 2
                for pair in gate_def.target_set
                if len(pair) == 2
            }

But this is far from ideal. It would be much better to have something like cirq.google.Sycamore.edges() or something similar.

Additional context (e.g. screenshots)

Maybe the generic GraphDevice is the answer to this as well?

What is the urgency from your perspective for this issue? Is it blocking important work?

P3 - I'm not really blocked by it, it is an idea I'd like to discuss / suggestion based on principle (because I have a workaround at the moment)

balopat commented 3 years ago

The above workaround doesn't work for Bristlecone, it uses a completely different way to display the ascii diagram based on neighbors_of:

    def neighbors_of(self, qubit: GridQubit):
        """Returns the qubits that the given qubit can interact with."""
        possibles = [
            GridQubit(qubit.row + 1, qubit.col),
            GridQubit(qubit.row - 1, qubit.col),
            GridQubit(qubit.row, qubit.col + 1),
            GridQubit(qubit.row, qubit.col - 1),
        ]
        return [e for e in possibles if e in self.qubits]
maffoo commented 3 years ago

+1 for adding this to the device. For reference, we usually refer to "pairs" internally, rather than "edges", see our internal GridDevice implementation.