# A basic question on circuits and matrix representation

I have several (rather basic) questions on matrix representation of circuits and I would be very grateful to anyone that could clear up my confusion, thank you in advance.

1) When reading circuit diagrams I know that the input qubit goes in the left hand side. So If we are reading a circuit and it's gates go in the order a,b,c does that mean when we want to write out its matrix representation we multiply the matrices in the order c,a,b . For example if we have a circuit which consists of a swap gate followed by a Hadamard gate on the second qubit , followed by a Hadamard gate on the first qubit , then to calculate it's matrix representation we would have to calculate it in the order $$(H_1\otimes I)(I\otimes H_2)Swap$$, correct ? (as this reflects the order of application of gates on the qubit state).

2)If we are given a gate $$S=\begin{pmatrix}1 &0 \\0 &i \end{pmatrix}$$, but it is on the top line of a two line circuit with a line connecting it to the second (In other words it's a control gate ), I know that the first qubit is the control and the second is the target but I'm unsure of how to write it , should it be $$S=\begin{pmatrix}1 & 0 &0&0 \\ 0 &-i &0&0\\0&0&1&0\\0&0&0&-i \end{pmatrix}$$,

or should it be

$$S=\begin{pmatrix}1 & 0 &0&0 \\ 0 &1 &0&0\\0&0&-i&0\\0&0&0&-i \end{pmatrix}$$,

1. Yes, in the circuit the qubit "enters" to the left, and exits to the right, but when applying the gates to a state you must apply the one on the far left first, then the next and so on, so concretely you do write them down right to left, but it's just a consequence of writing the operator that we want to apply on the left of the vector, while our natural way of writing dictates a "time direction" left to right. As an example the following circuit

implements the operation

$$(I\otimes \mathrm{CNOT})(\mathrm{CNOT}\otimes I)(H\otimes I\otimes I)|000\rangle$$

1. A controlled gate does nothing if the first qubit is $$0$$ and performs the gate if it is $$1$$, you can represent it this way

$$\mathrm{CS}=|0\rangle\langle0|\otimes I + |1\rangle\langle1|\otimes S$$ Hence in matrix form it would be

$$\mathrm{CS}=\begin{pmatrix} I&0\\0&S \end{pmatrix} =\begin{pmatrix} 1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&i \end{pmatrix}$$

• Thank you for succinctly answering all my questions :) – bhapi Jul 31 '19 at 20:53