---
title: UnitaryGate (latest version)
description: API reference for qiskit.circuit.library.UnitaryGate in the latest version of qiskit
source: https://quantum.cloud.ibm.com/docs/en/api/qiskit/qiskit.circuit.library.UnitaryGate
---

# UnitaryGate

*class* `qiskit.circuit.library.UnitaryGate(data, label=None, check_input=True, *, num_qubits=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L39-L256)

Bases: [`Gate`](/docs/api/qiskit/qiskit.circuit.Gate "qiskit.circuit.gate.Gate")

Class for quantum gates specified by a unitary matrix.

Example:

We can create a unitary gate from a unitary matrix then add it to a quantum circuit. The matrix can also be directly applied to the quantum circuit, see [`QuantumCircuit.unitary()`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#unitary "qiskit.circuit.QuantumCircuit.unitary").

```python
from qiskit import QuantumCircuit
from qiskit.circuit.library import UnitaryGate

matrix = [[0, 0, 0, 1],
            [0, 0, 1, 0],
            [1, 0, 0, 0],
            [0, 1, 0, 0]]
gate = UnitaryGate(matrix)

circuit = QuantumCircuit(2)
circuit.append(gate, [0, 1])
```

**Parameters**

- **data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)  *|*[*Gate*](/docs/api/qiskit/qiskit.circuit.Gate "qiskit.circuit.Gate") *| BaseOperator*) – Unitary operator.
- **label** ([*str*](https://docs.python.org/3/library/stdtypes.html#str) *| None*) – Unitary name for backend \[Default: `None`].
- **check\_input** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If set to `False` this asserts the input is known to be unitary and the checking to validate this will be skipped. This should only ever be used if you know the input is unitary, setting this to `False` and passing in a non-unitary matrix will result unexpected behavior and errors.
- **num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – If given, the number of qubits in the matrix. If not given, it is inferred.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If input data is not an N-qubit unitary operator.

## Attributes

### base\_class

Get the base class of this instruction. This is guaranteed to be in the inheritance tree of `self`.

The “base class” of an instruction is the lowest class in its inheritance tree that the object should be considered entirely compatible with for \_all\_ circuit applications. This typically means that the subclass is defined purely to offer some sort of programmer convenience over the base class, and the base class is the “true” class for a behavioral perspective. In particular, you should *not* override [`base_class`](#qiskit.circuit.library.UnitaryGate.base_class "qiskit.circuit.library.UnitaryGate.base_class") if you are defining a custom version of an instruction that will be implemented differently by hardware, such as an alternative measurement strategy, or a version of a parametrized gate with a particular set of parameters for the purposes of distinguishing it in a [`Target`](/docs/api/qiskit/qiskit.transpiler.Target "qiskit.transpiler.Target") from the full parametrized gate.

This is often exactly equivalent to `type(obj)`, except in the case of singleton instances of standard-library instructions. These singleton instances are special subclasses of their base class, and this property will return that base. For example:

```python
>>> isinstance(XGate(), XGate)
True
>>> type(XGate()) is XGate
False
>>> XGate().base_class is XGate
True
```

In general, you should not rely on the precise class of an instruction; within a given circuit, it is expected that `Instruction.name` should be a more suitable discriminator in most situations.

### decompositions

Get the decompositions of the instruction from the SessionEquivalenceLibrary.

### definition

Return definition in terms of other basic gates.

### label

Return instruction label

### mutable

Is this instance is a mutable unique instance or not.

If this attribute is `False` the gate instance is a shared singleton and is not mutable.

### name

Return the name.

### num\_clbits

Return the number of clbits.

### num\_qubits

Return the number of qubits.

### params

The parameters of this `Instruction`. Ideally these will be gate angles.

## Methods

### add\_decomposition

`add_decomposition(decomposition)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L318-L323)

Add a decomposition of the instruction to the SessionEquivalenceLibrary.

### adjoint

`adjoint()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L130-L132)

Return the adjoint of the unitary.

### broadcast\_arguments

`broadcast_arguments(qargs, cargs)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/gate.py#L206-L263)

Validation and handling of the arguments and its relationship.

For example, `cx([q[0],q[1]], q[2])` means `cx(q[0], q[2]); cx(q[1], q[2])`. This method yields the arguments in the right grouping. In the given example:

```python
in: [[q[0],q[1]], q[2]],[]
outs: [q[0], q[2]], []
      [q[1], q[2]], []
```

The general broadcasting rules are:

> - If len(qargs) == 1:
>
>   ```python
>   [q[0], q[1]] -> [q[0]],[q[1]]
>   ```
>
> - If len(qargs) == 2:
>
>   ```python
>   [[q[0], q[1]], [r[0], r[1]]] -> [q[0], r[0]], [q[1], r[1]]
>   [[q[0]], [r[0], r[1]]]       -> [q[0], r[0]], [q[0], r[1]]
>   [[q[0], q[1]], [r[0]]]       -> [q[0], r[0]], [q[1], r[0]]
>   ```
>
> - If len(qargs) >= 3:
>
>   ```python
>   [q[0], q[1]], [r[0], r[1]],  ...] -> [q[0], r[0], ...], [q[1], r[1], ...]
>   ```

**Parameters**

- **qargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)) – List of quantum bit arguments.
- **cargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)) – List of classical bit arguments.

**Returns**

A tuple with single arguments.

**Raises**

[**CircuitError**](/docs/api/qiskit/circuit#qiskit.circuit.CircuitError "qiskit.circuit.CircuitError") – If the input is not valid. For example, the number of arguments does not match the gate expectation.

**Return type**

[*Iterable*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable)\[[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[list](https://docs.python.org/3/library/stdtypes.html#list), [list](https://docs.python.org/3/library/stdtypes.html#list)]]

### conjugate

`conjugate()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L126-L128)

Return the conjugate of the unitary.

### control

`control(num_ctrl_qubits=1, label=None, ctrl_state=None, annotated=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L182-L242)

Return a controlled version of itself.

The controlled gate is implemented as [`ControlledGate`](/docs/api/qiskit/qiskit.circuit.ControlledGate "qiskit.circuit.ControlledGate") when `annotated` is `False`, and as [`AnnotatedOperation`](/docs/api/qiskit/qiskit.circuit.AnnotatedOperation "qiskit.circuit.AnnotatedOperation") when `annotated` is `True`.

**Parameters**

- **num\_ctrl\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – Number of controls to add. Defaults to `1`.
- **label** ([*str*](https://docs.python.org/3/library/stdtypes.html#str) *| None*) – Optional gate label. Defaults to `None`. Ignored if the controlled gate is implemented as an annotated operation.
- **ctrl\_state** ([*int*](https://docs.python.org/3/library/functions.html#int)  *|*[*str*](https://docs.python.org/3/library/stdtypes.html#str) *| None*) – The control state of the gate, specified either as an integer or a bitstring (e.g. `"110"`). If `None`, defaults to the all-ones state `2**num_ctrl_qubits - 1`.
- **annotated** ([*bool*](https://docs.python.org/3/library/functions.html#bool) *| None*) – Indicates whether the controlled gate should be implemented as a controlled gate or as an annotated operation. If `None`, treated as `False`.

**Returns**

A controlled version of this gate.

**Return type**

[*ControlledGate*](/docs/api/qiskit/qiskit.circuit.ControlledGate "qiskit.circuit.controlledgate.ControlledGate") | [*AnnotatedOperation*](/docs/api/qiskit/qiskit.circuit.AnnotatedOperation "qiskit.circuit.annotated_operation.AnnotatedOperation")

### copy

`copy(name=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L428-L443)

Copy of the instruction.

**Parameters**

**name** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) – name to be given to the copied circuit, if `None` then the name stays the same.

**Returns**

a copy of the current instruction, with the name updated if it was provided

**Return type**

[qiskit.circuit.Instruction](/docs/api/qiskit/qiskit.circuit.Instruction "qiskit.circuit.Instruction")

### inverse

`inverse(annotated=False)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L122-L124)

Return the adjoint of the unitary.

**Parameters**

**annotated** ([*bool*](https://docs.python.org/3/library/functions.html#bool))

### is\_parameterized

`is_parameterized()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L283-L288)

Return whether the `Instruction` contains [compile-time parameters](/docs/api/qiskit/circuit#circuit-compile-time-parameters).

### power

`power(exponent, annotated=False)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/gate.py#L63-L94)

Raise this gate to the power of `exponent`.

Implemented either as a unitary gate (ref. [`UnitaryGate`](#qiskit.circuit.library.UnitaryGate "qiskit.circuit.library.UnitaryGate")) or as an annotated operation (ref. [`AnnotatedOperation`](/docs/api/qiskit/qiskit.circuit.AnnotatedOperation "qiskit.circuit.AnnotatedOperation")). In the case of several standard gates, such as [`RXGate`](/docs/api/qiskit/qiskit.circuit.library.RXGate "qiskit.circuit.library.RXGate"), when the power of a gate can be expressed in terms of another standard gate that is returned directly.

**Parameters**

- **exponent** ([*float*](https://docs.python.org/3/library/functions.html#float)) – the power to raise the gate to
- **annotated** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – indicates whether the power gate can be implemented as an annotated operation. In the case of several standard gates, such as [`RXGate`](/docs/api/qiskit/qiskit.circuit.library.RXGate "qiskit.circuit.library.RXGate"), this argument is ignored when the power of a gate can be expressed in terms of another standard gate.

**Returns**

An operation implementing `gate^exponent`

**Raises**

[**CircuitError**](/docs/api/qiskit/circuit#qiskit.circuit.CircuitError "qiskit.circuit.CircuitError") – If gate is not unitary

### repeat

`repeat(n)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L491-L521)

Creates an instruction with `self` repeated $n$ times.

**Parameters**

**n** ([*int*](https://docs.python.org/3/library/functions.html#int)) – Number of times to repeat the instruction

**Returns**

Containing the definition.

**Return type**

[qiskit.circuit.Instruction](/docs/api/qiskit/qiskit.circuit.Instruction "qiskit.circuit.Instruction")

**Raises**

[**CircuitError**](/docs/api/qiskit/circuit#qiskit.circuit.CircuitError "qiskit.circuit.CircuitError") – If n \< 1.

### reverse\_ops

`reverse_ops()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L345-L369)

For a composite instruction, reverse the order of sub-instructions.

This is done by recursively reversing all sub-instructions. It does not invert any gate.

**Returns**

**a new instruction with**

sub-instructions reversed.

**Return type**

[qiskit.circuit.Instruction](/docs/api/qiskit/qiskit.circuit.Instruction "qiskit.circuit.Instruction")

### soft\_compare

`soft_compare(other)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L214-L254)

Soft comparison between gates. Their names, number of qubits, and classical bit numbers must match. The number of parameters must match. Each parameter is compared. If one is a ParameterExpression then it is not taken into account.

**Parameters**

**other** (*instruction*) – other instruction.

**Returns**

are self and other equal up to parameter expressions.

**Return type**

[bool](https://docs.python.org/3/library/functions.html#bool)

### to\_matrix

`to_matrix()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/gate.py#L49-L61)

Return a Numpy.array for the gate unitary matrix.

**Returns**

if the Gate subclass has a matrix definition.

**Return type**

np.ndarray

**Raises**

[**CircuitError**](/docs/api/qiskit/circuit#qiskit.circuit.CircuitError "qiskit.circuit.CircuitError") – If a Gate subclass does not implement this method an exception will be raised when this base class method is called.

### to\_mutable

`to_mutable()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/instruction.py#L144-L152)

Return a mutable copy of this gate.

This method will return a new mutable copy of this gate instance. If a singleton instance is being used this will be a new unique instance that can be mutated. If the instance is already mutable it will be a deepcopy of that instance.

### transpose

`transpose()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L134-L136)

Return the transpose of the unitary.

### validate\_parameter

`validate_parameter(parameter)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/circuit/library/generalized_gates/unitary.py#L251-L256)

Unitary gate parameter has to be an ndarray.
