# czGate

Controlled Z gate

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

## Syntax

``g = czGate(controlQubit,targetQubit)``

## Description

example

````g = czGate(controlQubit,targetQubit)` applies a controlled Z gate to a target qubit based on the state of a control qubit and returns a `quantum.gate.SimpleGate` object. If the control qubit is in the $|0〉$ state, then this gate does nothing. If the control qubit is in the $|1〉$ state, then this gate applies a Pauli Z gate (`zGate`) to the target qubit. If `controlQubit` and `targetQubit` are vectors of the same length, `cyGate` returns a column vector of gates, where `g(i)` represents a controlled Z gate applied to a control qubit with index `controlQubit(i)` and a target qubit with index `targetQubit(i)`. If either `controlQubit` or `targetQubit` is a scalar, and the other input is a vector, then MATLAB® expands the scalar to match the size of the vector input.The qubit indices in each element pair of `controlQubit` and `targetQubit` must not be the same. ```

## Examples

collapse all

Create a controlled Z gate that acts on a control qubit with index 1 and a target qubit with index 2.

`g = czGate(1,2)`
```g = SimpleGate with properties: Type: "cz" ControlQubits: 1 TargetQubits: 2 Angles: [1×0 double]```

Get the matrix representation of the gate.

`M = getMatrix(g)`
```M = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 -1```

Create an array of three controlled Z gates. The first gate acts on control qubit 1 and target qubit 2, the next gate acts on control qubit 2 and target qubit 3, and the final gate acts on control qubit 3 and target qubit 4.

`g = czGate(1:3,2:4)`
```g = 3×1 SimpleGate array with gates: Id Gate Control Target 1 cz 1 2 2 cz 2 3 3 cz 3 4 ```

## Input Arguments

collapse all

Control qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `2`

Example: `6:8`

Target qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `1`

Example: `3:5`

collapse all

### Matrix Representation of Controlled Z Gate

The matrix representation of a controlled Z gate applied to control qubit 1 and target qubit 2 is

`$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 1& 0\\ 0& 0& 0& -1\end{array}\right].$`

If the control qubit is in the $|0〉$ state, then this gate leaves the target qubit as is. If the control qubit is in the $|1〉$ state, then this gate applies a Pauli Z gate to the target qubit. Because this gate affects only the $|11〉$ state, swapping the control and target qubits does not change the gate operation.

## Version History

Introduced in R2023a