Operation operates on and what dimension each qudit must be. This method is used to determine how many qudits the gate or cirq.qid_shape and def _qid_shape_Ī qudit gate or operation must implement the _qid_shape_ method that returns a This method returns an integer and is used by cirq.num_qubits to determine Arithmetic functions such as _pow_, _mul_, _add_ define theĪ Gate must implement the _num_qubits_ (or _qid_shape_) method.Of sub-values that can be compared for equality. YouĬan also use _equality for objects that have a small list _eq_ and _hash_ which define whether objects are equal or not._str_ for user-friendly string output and _repr_ is the Python-friendly string output, meaning that eval(repr(y))=y should always be true.There are many standard magic methods in Python. _qid_shape_ magic method to define the number of qubits (or qudits) used They are defined below.Īt minimum, you will need to define either the _num_qubits_ or Other uses are specific to cirq and are found in the Two underscores and used by Cirq's protocols or built-in Python methods.įor instance, Python translates cirq.Z**0.25 intoĬirq.Z._pow_(0.25). Methods." Much of Cirq relies on "magic methods", which are methods prefixed with one or Qubits: (cirq.LineQubit(0), cirq.LineQubit(1))Īs mentioned, methods such as _unitary_ which we have seen are known as "magic We can simulate this circuit and verify it indeed swaps the qubits. As mentioned, this can be useful for interpreting circuits at a higher level than individual (primitive) gates. Instead, the _circuit_diagram_info_ appears in the circuit. When we use this gate in a circuit, the individual gates in the decomposition do not appear in the circuit. (One can also return a list of operations instead of a generator.) The _decompose_ method yields the operations which implement the custom gate. We show an example below of a custom swap gate defined from a known decomposition of three CNOT gates. This is useful, for example, when groups of gates appear repeatedly in a circuit, or when a standard decomposition of a gate into primitive gates is known. Print("Circuit with a custom rotation gate:")Ĭustom gates can also be defined from a known decomposition (of gates). RotationGate(theta=0.1).on(cirq.LineQubit(0)) This gate can be used in a circuit as shown below. """Define a custom gate with a parameter.""" Here, we define a gate which corresponds to the unitary One can create a custom Cirq gate from a unitary matrix in the following manner. We demonstrate these patterns via the following examples. More information about magic methods is included at the end of this guide. Note: Methods beginning and ending with one or more underscores are magic methods and are used by Cirq's protocols or built-in Python functions. Define one of the _unitary_ or _decompose_ methods.Define one of the _num_qubits_ or _qid_shape_ methods.To define custom gates, we inherit from a base gate class and define a few methods. To use a unitary which is not a standard gate in a circuit, one can create a custom gate as described in this guide. Standard gates such as Pauli gates and CNOTs are defined in cirq.ops as described here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |