question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to determine if an operation has a gate?

See original GitHub issue

It used to be that you could check whether an operation had a gate using a type check isinstance(gate_op, cirq.GateOperation) But if you tag an op it loses that signature. isinstance(gate_op.with_tags('you are it'), cirq.GateOperation) == False

Note that there are a lot of these checks throughout Cirq that will presumably break when you tag an operation. For example look at cirq.EjectPhasedPaulis().

eject = cirq.EjectPhasedPaulis()
q = cirq.NamedQubit('a')
circuit = cirq.Circuit(cirq.PhasedXPowGate(phase_exponent=0.125)(q), cirq.Z(q))
eject.optimize_circuit(circuit)
print(circuit)

does the correct optimization

a: ───PhX(0.625)───────

Put if you tag the Z, it doesn’t do the optimization

circuit = cirq.Circuit(cirq.PhasedXPowGate(phase_exponent=0.125)(q), cirq.Z(q).with_tags('you are it')
eject.optimize_circuit(circuit)
print(circuit)

prints

a: ───PhX(0.125)───Z['you are it']───

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
95-martin-orioncommented, Dec 22, 2020

A similar issue exists for CircuitOperations (#3580), which do not populate the gate field but instead have a circuit field not present in the base Operation class.

1reaction
maffoocommented, Dec 1, 2020

cirq.Operation has a gate property of type Optional[Gate] so you can check it against None on any operation:

if op.gate is not None:
    ...

Note that TaggedOperation delegates gate to the underlying operation (https://github.com/quantumlib/Cirq/blob/master/cirq/ops/raw_types.py#L534) so it also works fine to access the gate of a tagged operation directly without going through the sub operation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Designing a circuit to verify operation of an OR gate.
This can be found and verified using demorgan's theorem. So, if both functions listed produce the same output, then the gate is working....
Read more >
Gate Operation - an overview | ScienceDirect Topics
The operation of a dynamic gate is typically divided into two major phases: precharge and evaluation. The mode of operation is determined by...
Read more >
Logic OR Gate Tutorial
Logic OR Gates are available using digital circuits to produce the desired logical function and is given a symbol whose shape represents the...
Read more >
logic gate (AND, OR, XOR, NOT, NAND, NOR and XNOR)
The NAND gate operates as an AND gate followed by a NOT gate. It acts in the manner of the logical operation "and"...
Read more >
Logic Gates - Building an ALU
At the end of this tutorial you will find some activities that you will need to complete ... was there a carry, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found