Formal class 'qstate' [package "qsimulatR"] with 5 slots
..@ nbits : int 4
..@ coefs : cplx [1:16] 1+0i 0+0i 0+0i ...
..@ basis : chr [1:16] "|0000>" "|0001>" "|0010>" "|0011>" ...
..@ noise :List of 4
.. ..$ p : num 0
.. ..$ bits : int [1:4] 1 2 3 4
.. ..$ error: chr "any"
.. ..$ args : list()
..@ circuit:List of 2
.. ..$ ncbits : num 0
.. ..$ gatelist: list()
Playing With Qubits
I think I will just play around with some qubits and various gates for fun here. I will use the qsimulatR package to do the manipulations. Firstly, I will create a 4-qubit state like,
ket0 <- qstate(4)
which gives me this,
( 1 ) * |0000>
So, if we were to plot this, it wouldn’t be very interesting, just four lines or qubits. If we examine the structure, str(ket0), it would be,
Now, if we applied a Hadamard1 gate to the first qubit, for example, we would see the circuit below,

The formula for the Hadamard transform on N qubits is,
\[H^{\otimes N}=\frac{1}{\sqrt{2^N}}\sum_{x,y\in\{0,1\}^N}(-1)^{x\cdot y}|y\rangle\langle x|\]
and,
\[x\cdot y=\sum_{i=1}^Nx_iy_i\]
where the \(\otimes\) denotes the Kronecker product.
Now, if we wanted to see a Hadamard matrix to use on 4-bit matrices, we could, using pracma::hadamard(x) where x=4, manually construct the following,
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 -1 1 -1
[3,] 1 1 -1 -1
[4,] 1 -1 -1 1
and then, using \(\frac{1}{\sqrt{N}} \times matrix\), end with the unitary (U) matrix,
[,1] [,2] [,3] [,4]
[1,] 0.5 0.5 0.5 0.5
[2,] 0.5 -0.5 0.5 -0.5
[3,] 0.5 0.5 -0.5 -0.5
[4,] 0.5 -0.5 -0.5 0.5
And conversely, this unitary matrix becomes a complex Hadamard when multiplied by \(\sqrt{N}\),
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 -1 1 -1
[3,] 1 1 -1 -1
[4,] 1 -1 -1 1
And now we’ve gone full circle. But this is not the same as working on quantum qubits with a Hadamard gate.
So, going back a bit, if we then had a single-qubit of states |0> and |1>2 and applied a single H gate to it, we would see this sequence,
a <- hadamard(2)
b <- matrix(c(0,1),2,1)
1/sqrt(2)*a %*% b [,1]
[1,] 0.7071068
[2,] -0.7071068
Compare this with the below using H(1) * qstate(1),
( 0.7071068 ) * |0>
+ ( 0.7071068 ) * |1>
As the Hadamard gate acts on a single qubit, to perform a Hadamard transform on the 4-qubit ket0, we could use ket1 <- H(4) * (H(3) * (H(2) * (H(1) * ket0))) for the 4-bit superposition with the circuit diagram below that,
( 0.25 ) * |0000>
+ ( 0.25 ) * |0001>
+ ( 0.25 ) * |0010>
+ ( 0.25 ) * |0011>
+ ( 0.25 ) * |0100>
+ ( 0.25 ) * |0101>
+ ( 0.25 ) * |0110>
+ ( 0.25 ) * |0111>
+ ( 0.25 ) * |1000>
+ ( 0.25 ) * |1001>
+ ( 0.25 ) * |1010>
+ ( 0.25 ) * |1011>
+ ( 0.25 ) * |1100>
+ ( 0.25 ) * |1101>
+ ( 0.25 ) * |1110>
+ ( 0.25 ) * |1111>

We can check to see if it is valid using sum(Mod(ket1@coefs)^2),
[1] 1
What’s fun is we can reverse this entire process with H(4) * (H(3) * (H(2) * (H(1) * ket1))), which supports the premise that all gates must be reversible, so no information is lost,
( 1 ) * |0000>

In other words, we have done the Hadamard gate function twice. This follows Landauer’s principle3 of fundamental thermodynamic constraints for classical and quantum information processing. As a loose analogy, think of any classical gate, such as the AND gate, where two inputs are fed into it, but only one is saved; the other is dissipated as heat.
And now I think we are finished with the eye-glaze portion. Have a great day, and we praise God for all things!
Footnotes
Also called a Walsh-Hadamard gate, named after Jacques Hadamard and Joseph L. Walsh↩︎
Bra-ket (or Dirac) notation was created by Paul Dirac in his 1939 publication A New Notation for Quantum Mechanics. The notation was introduced as an easier way to write quantum mechanical expressions.[1] The name comes from the English word “Bracket”. ↩︎
Landauer’s principle is a physical principle pertaining to the lower theoretical limit of energy consumption of computation. It holds that an irreversible change in information stored in a computer, such as merging two computational paths, dissipates a minimum amount of heat to its surroundings.↩︎