Category Archives: Programming

Programming is the process of designing, coding, debugging and maintaining the source code of programs

Sign-Magnitude

Sign-Magnitude

The sign-magnitude representation uses the most significant bit as a sign bit, making it easier to check whether the integer is positive or negative

There are several conventions for representing integers in a machine in their binary form, both positive and negative

They all try to imply the most significant bit (the one to the leftmost) of the word (1 word equals 8 bits) as a sign bit

In a word of n bits, the n-1 bits on the right represent the magnitude of the integer

The general case would be to apply the formula:


A=\begin{cases}\sum\limits_{i=0}^{n} 0^{n-2} \cdot a_i & \text{if }a_{n-1}=0 \\ -\sum\limits_{i=0}^{n} 0^{n-2} \cdot a_i & \text{if }a_{n-1}=1 \end{cases}


The signo-magnitude representation has several limitations:

  • Addition and subtraction require to take into account both the signs of the numbers as their relative magnitudes to carry out the operation in question
  • There are two representations for the number 0:


    \begin{cases} +0_{(10}=00000000_{(2} \\ -0_{(10}=10000000_{(2} \end{cases}

Because of these limitations, signo-magnitude representation is rarely used to represent integers on a machine

Instead, the most common scheme is representation two's complement

Conversions

Example: Convert from decimal to sign-magnitude

We're going to convert \pm 18 to sign-magnitude, first we convert it to binary

Number Quotient Rest
\frac{18}{2} 9 0
\frac{9}{2} 4 1
\frac{4}{2} 2 0
\frac{2}{2} 1 0

So we have that the binary part is:


18_{(10} = 10010_{(2}


Now we apply the distinction by sign within 1 word (remember that they are 8 bits), then we have:


\begin{cases} +18_{(10}=00010010_{(2} \\ -18_{(10}=10010010_{(2} \end{cases}

Two's complement

Two's complement

The two-in-two representation, like the sign-magnitude representation, uses the most significant bit as a sign bit, making it easier to check whether the integer is positive or negative

Differs from the representation in sign-magnitude in the way of interpreting the bits remaining

Consider an n-bit A integer represented in add-on to two. If A is positive, the sign bit, a_{n-1} is 0. The remaining bits represent the magnitude of the number in the same way as in the sign-magnitude representation, i.e.:

\sum\limits_{i=0}^{n} 0^{n-2} \cdot a_i\text{ si }A\geq 0

The number 0 is identified as positive and therefore has a bit of sign 0 and a magnitude of all zeros. The range of positive integers that can be represented is, from 0 (all bits are 0) to 2^{n-1}-1 (all bits are 1). Any larger number would require more bits

For a negative A number, the sign bit, a_n-1 is 1. The remaining n-1 bits can take any of the 2^{n-1} Combinations. Therefore, negative integers can be represented from -1 to (-2)^{n-1}, I mean:

A=(-2)^{n-1}\cdot a_{n-1}

It would be desirable to assign the bits of negative integers in such a way that their arithmetic manipulation could be done in a direct way, similar to that of unsigned integers. In unsigned representation, to calculate the value of an integer from its bit expression, the most significant weight is 2^{n-1}

The general case would be to apply the formula:

A=(-2)^{n-1} \cdot a_{n-1} + \sum\limits_{i=0}^{n} 0^{n-2} \cdot a_i

Properties

  • Range of values
    (-2)^{n-1}\text{ a } 2^{n-1}-1
  • Number of representations of 0
    Only
  • Denial
    Run the Boolean plug-in for each bit and add 1 to the resulting bit pattern
  • Enlargement of the length in bits
    Extra positions are added to the left, filling them with the value of the original sign bit
  • Rule of overflow
    If two equal sign numbers (both positive or negative) are added, there is overflow if and only if, the result has an opposite sign
  • Rule for subtraction
    To subtract B from A, the complement is taken to two of B and added to A

Conversions

Example: Convert from decimal to two's complement

We're going to convert \pm 18 to two's complement, first we convert it to binary

Number Ratio Rest
\frac{18}{2} 9 0
\frac{9}{2} 4 1
\frac{4}{2} 2 0
\frac{2}{2} 1 0

So we have that the binary part is:

18_{(10} = 10010_{(2}

To find the -18 you have to use the denial of the 18

It runs the Boolean plugin of each bit within 1 word (remember that it is 8 bits), then we have:

18_{(10} = 00010010_{(2}
00010010 \rightarrow 11101101

Adds 1 to the resulting bit pattern

11101101+1=11101110

So we have to:

\begin{cases} 18_{(10} = 00010010_{(2} \\ -18_{(10} = 11101110_{(2} \end{cases}

Floating-point

Floating-point

The most important floating-point representation is that defined in IEEE 754 (ANSI/IEEE Std 754-1985)

This standard was developed to facilitate the portability of programs from one processor to another and to encourage the development of programs, sophisticated numerical

This standard has been widely adopted and is used practically in all processors and coprocessors arithmetic current

There are two floating-point representations:

  • Simple format (32 bit)
  • Dual format (64 bit)

Simple format (32 bit)

The following format is used in the simple format to store a word (32 bit):

Sign Bit Exponent Mantissa
1 bit 8 bits 23 bits

Given a E_1=\text{ exponent } + 127 (127 is the maximum positive number of the exponent) we have to:

00000000_{(2}\leq E_1\leq 11111111_{(2}

If we denote (E1)_{(10} to conversion decimal E_1, we need to:

0\leq (E1)_{(10}\leq 2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0=255

Or what would be the same:

-127\leq (E_1)_{(10} -127\leq 128

If we denote \alpha=(E_1)_{(10} -127 we have to:

-127\leq\alpha\leq 128
(E_1)=\alpha+127

We denoted s as the sign bit and M as the mantissa. As a result, two types of numbers arise:

  • Numbers normal
    -127 < \alpha < 128

    Conversion to normal number: (-1)\cdot s \cdot 1,M \cdot 2^\alpha

  • Numbers subnormal
    \alpha = (-127)

    Conversion to subnormal number: (-1)\cdot s \cdot 0,M \cdot 2^{-126}

  • \alpha = 128

    Gives rise to exceptions of type +\infty, -\infty\text{ y NaN} (not a number)

A bit hidden in the mantissa is used. You take the first digit that is always a_1 = 1. This hidden bit will not need to be stored, thus getting a few more numbers

Number Decimal Binary
\tiny \text{Maximum number normal positive} \tiny 3,40282347 \cdot 10^{38} \tiny\text{0 11111110 11111111111111111111111}
\tiny \text{Maximum number normal negative} \tiny -3,40282347 \cdot 10^{38} \tiny \text{1 11111110 11111111111111111111111}
\tiny \text{Minimum number normal positive} \tiny 1,17549435 \cdot 10^{-38} \tiny \text{0 00000001 00000000000000000000000}
\tiny \text{Minimum number normal negative} \tiny -1,17549435 \cdot 10^{-38} \tiny \text{1 00000001 00000000000000000000000}
\tiny \text{Maximum number subnormal positive} \tiny 1,17549421 \cdot 10[{-38} \tiny \text{0 00000000 11111111111111111111111}
\tiny \text{Maximum number subnormal negative} \tiny -1,17549421 \cdot 10[{-38} \tiny \text{1 00000000 11111111111111111111111}
\tiny \text{Minimum number subnormal positive} \tiny 1,40129846 \cdot 10^{-45} \tiny \text{0 00000000 00000000000000000000001}
\tiny \text{Minimum number subnormal negative} \tiny -1,40129846 \cdot 10^{-45} \tiny \text{1 00000000 00000000000000000000001}
\tiny \text{+0} \tiny 0,0 \tiny \text{0 00000000 00000000000000000000000}
\tiny \text{-0} \tiny -0,0 \tiny \text{1 00000000 00000000000000000000000}
\tiny +\infty \tiny +\infty \tiny \text{0 11111111 00000000000000000000000}
\tiny -\infty \tiny -\infty \tiny \text{1 11111111 00000000000000000000000}
\tiny \text{NaN} \tiny NaN \tiny \text{(0 or 1) 11111111 (some 1)}

Conversions

Example: converting the 3737 to simple format (32 bit)

It's positive, so the sign is 0

Number Ratio Rest
\frac{3737}{2} 1868 1
\frac{1868}{2} 934 0
\frac{934}{2} 467 0
\frac{467}{2} 233 1
\frac{233}{2} 116 1
\frac{116}{2} 58 0
\frac{58}{2} 29 0
\frac{29}{2} 14 1
\frac{14}{2} 7 0
\frac{7}{2} 3 1
\frac{3}{2} 1 1

So we have that the binary part is:

3737_{(10} = 111010011001_{(2}

For the exponent we will need to move 12 decimal places, therefore we must make 127 + 11 x 138 (we add 11 because the hidden bit is not counted)

Number Ratio Rest
\frac{138}{2} 69 0
\frac{69}{2} 34 1
\frac{34}{2} 17 0
\frac{17}{2} 8 1
\frac{8}{2} 4 0
\frac{4}{2} 2 0
\frac{2}{2} 1 0

So we have that the exponent is:

138_{(10} = 10001010_{(2}

So we have to:

3737_{(10} = \text{0 10001010 11010011001000000000000}_{(2}

Dual format (64 bit)

The following format is used in the simple format to store two words (64 bit):

Sign Bit Exponent Mantissa
1 bit 11 bits 52 bits

Given a E_1=\text{ exponent } + 1023 (1023 is the maximum positive number of the exponent) we have to:

00000000000_{(2}\leq E_1\leq 11111111111_{(2}

If we denote (E1)_{(10} to conversion decimal E_1, we need to:

0\leq (E1)_{(10}\leq 2^{10}+2^9+2^8+2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0=2047

Or what would be the same:

-1023\leq (E_1)_{(10} -1023\leq 1024

If we denote \alpha=(E_1)_{(10} -1023 we have to:

-1023\leq\alpha\leq 1024
(E_1)=\alpha+1023

We denoted s as the sign bit and M as the mantissa. As a result, two types of numbers arise:

  • Numbers normal
    -1023 < \alpha < 1024

    Conversion to normal number: (-1)\cdot s \cdot 1,M \cdot 2^\alpha

  • Numbers subnormal
    \alpha = (-1023)

    Conversion to subnormal number: (-1)\cdot s \cdot 0,M \cdot 2^{-1022}

  • \alpha = 1024

    Gives rise to exceptions of type +\infty, -\infty\text{ y NaN} (not a number)

A bit hidden in the mantissa is used. You take the first digit that is always a_1 = 1. This hidden bit will not need to be stored, thus getting a few more numbers

Number Decimal Binary
\tiny \text{Maximum number}\\ \text{normal positive} \tiny 1,7976931 \cdot 10^{308} \tiny \text{0 11111111110 1111111111111111111111111111111111111111111111111111}
\tiny \text{Maximum number}\\ \text{normal negative} \tiny -1,7976931 \cdot 10^{308} \tiny \text{1 11111111110 1111111111111111111111111111111111111111111111111111}
\tiny \text{Minimum number}\\ \text{normal positive} \tiny 2,2250738 \cdot 10^{-308} \tiny \text{0 00000000001 0000000000000000000000000000000000000000000000000000}
\tiny \text{Minimum number}\\ \text{normal negative} \tiny -2,2250738 \cdot 10^{-308} \tiny \text{1 00000000001 0000000000000000000000000000000000000000000000000000}
\tiny \text{Maximum number}\\ \text{subnormal positive} \tiny 2,2250738 \cdot 10^{-308} \tiny \text{0 00000000000 1111111111111111111111111111111111111111111111111111}
\tiny \text{Maximum number}\\ \text{subnormal negative} \tiny -2,2250738 \cdot 10^{-308} \tiny \text{1 00000000000 1111111111111111111111111111111111111111111111111111}
\tiny \text{Minimum number}\\ \text{subnormal positive} \tiny 4,9406564 \cdot 10^{-324} \tiny \text{0 00000000000 0000000000000000000000000000000000000000000000000001}
\tiny \text{Minimum number}\\ \text{subnormal negative} \tiny -4,9406564 \cdot 10^{-324} \tiny \text{1 00000000000 0000000000000000000000000000000000000000000000000001}
\tiny +0 \tiny 0,0 \tiny \text{0 00000000000 0000000000000000000000000000000000000000000000000000}
\tiny -0 \tiny -0,0 \tiny \text{1 00000000000 0000000000000000000000000000000000000000000000000000}
\tiny +\infty \tiny +\infty \tiny \text{0 11111111111 0000000000000000000000000000000000000000000000000000}
\tiny -\infty \tiny -\infty \tiny \text{1 11111111111 0000000000000000000000000000000000000000000000000000}
\tiny \text{NaN} \tiny \text{NaN} \tiny \text{(0 or 1) 11111111111 (some 1)}

Conversions

Example: converting the 3737 to double format (64 bit)

It's positive, so the sign is 0

Number Ratio Rest
\frac{3737}{2} 1868 1
\frac{1868}{2} 934 0
\frac{934}{2} 467 0
\frac{467}{2} 233 1
\frac{233}{2} 116 1
\frac{116}{2} 58 0
\frac{58}{2} 29 0
\frac{29}{2} 14 1
\frac{14}{2} 7 0
\frac{7}{2} 3 1
\frac{3}{2} 1 1

So we have that the binary part is:

3737_{(10} = 111010011001_{(2}

For the exponent we will need to move 12 decimal places, therefore we must do 1023 + 11 x 1034 (we add 11 because the hidden bit is not counted)

Number Ratio Rest
\frac{1034}{2} 517 0
\frac{517}{2} 258 1
\frac{258}{2} 129 0
\frac{129}{2} 64 1
\frac{64}{2} 32 0
\frac{32}{2} 16 0
\frac{16}{2} 8 0
\frac{8}{2} 4 0
\frac{4}{2} 2 0
\frac{2}{2} 1 0

So we have that the exponent is:

138_{(10} = 10000001010_{(2}

So we have to:

\tiny 3737_{(10} = \text{0 10000001010 1101001100100000000000000000000000000000000000000000}_{(2}

Boolean algebra

Boolean algebra

The Boolean algebra is a discipline that analyzes the digital circuits and other digital systems

It is named after the English mathematician George Boole, who proposed the basic principles of this algebra in 1854 in his treatise An investigation of the laws of thought on wich to found the mathematical theories of logic and probabilities

In 1938, Claude Shannon, an assistant researcher in the M.I.T. Department of Electrical Engineering, suggested that Boole algebra could be used to solve switching circuit problems

Shannon's techniques were used, consequently in the analysis and design of digital circuits. Boole algebra is a useful tool in two areas:

  • Analysis
    It is a concise way of discovering the operation of digital circuits
  • Design
    Given a desired function, Boole algebra can be applied to develop a simplified complexity implementation of that function

Boole algebra uses variables and logical operations. A variable can take the value 1 when it is true or 0 when false. It has only three basic logical operations (AND, OR, and NOT), which are symbolically represented:

AND OR NOT
A AND B A OR B NOT TO
A\wedge B A\vee B \neg A

Truth tables

The truth table defines the basic logical operations, listing the value for each possible combination of the operand values. In addition to the basic operators of Boole algebra, three useful operators are also typically listed: XOR, XNOR, NAND, and NOR. Which can be obtained by trading with basic traders

Table AND

The AND operation is true if and only if the two operands are true

A B A\wedge B
0 0 0
0 1 0
1 0 0
1 1 1

Table OR

The OR operation is true if and only if one or both operands are true

A B A\vee B
0 0 0
0 1 1
1 0 1
1 1 1

Table NOT

The NOT operation inverts the value of the operand

A \neg A
0 1
1 0

Table XOR

The XOR operation is true if and only one of the operands is true

It is equivalent to performing:

(A \wedge \neg B) \vee (\neg A \wedge B) \equiv (A \vee B) \wedge (\neg A \vee \neg B)

A B A\oplus B
0 0 0
0 1 1
1 0 1
1 1 0

Table XNOR

The operation XNOR is the negation of the XOR operation

It is equivalent to performing:

(A \wedge B) \vee (\neg A \wedge \neg B)

A B \neg(A\oplus B)
0 0 1
0 1 0
1 0 0
1 1 1

Table NAND

The operation NAND is the negation of the AND operation

It is equivalent to performing:

\neg(A \wedge B)

A B \neg(A \wedge B)
0 0 1
0 1 1
1 0 1
1 1 0

Table NOR

The operation NOR is the negation of the OR operation

It is equivalent to performing:

\neg(A \vee B)

A B \neg(A \vee B)
0 0 1
0 1 0
1 0 0
1 1 0

Basic identities

There are two kinds of identities:

  • Basic rules (postulated)
    You assert without demonstration
  • Other identities
    Can be derived from the basic postulates

The postulates define the way in which boolean expressions are interpreted

Basic rules
A \wedge B = B \wedge A A \vee B = B \vee A Law commutative
A \wedge (B \vee C) = (A \wedge B) \vee (A \wedge C) A \vee (B \wedge C) = (A \vee B) \wedge (A \vee C) Law distributive
1 \wedge A = A 0 \vee A = A Neutral element
A \wedge \neg A = 0 A \vee \neg A = 1 Complementary element
Other identities
0 \wedge A = 0 1 \vee A = 1
A \wedge A = A A \vee A = A
A \wedge (B \wedge C) = (A \wedge B) \wedge C A \vee (B \vee C) = (A \vee B) \vee C Law associative
\neg (A \wedge B) = \neg A \vee \neg B \neg (A \vee B) = \neg A \wedge \neg B Theorem of Morgan

Qubits

Qubits

A qubit is a quantum system with two eigenstates that can be manipulated arbitrarily

It can only be correctly described by quantum mechanics, and only has two states that are well distinguishable by physical measurements

Qubits are also understood as the information contained in this quantum system of two possible states

In this sense, the qubit is the minimum and therefore constitutive unit of quantum information theory

It is a fundamental concept for quantum programming and for quantum cryptography, the quantum analogue of bit in computing

These qubits no longer simply take integer values ​​(0 or 1)

But now they are represented by mathematical entities called state vectors |\psi\succ

What are we going to represent using the Dirac notation:

\begin{matrix} |0\succ&=\binom{1}{0}\\ |1\succ&=\binom{0}{1} \end{matrix}

What defines the basis states of a qubit

As a vector, the state |\psi\succ of each qubit belongs to a particular vector space called Hilbert space

Physically, we can measure the state |\psi\succ in two orthogonal basic states, which by convention are called |0\succ and |1\succ

This implies that the Hilbert space is 2 dimensional

So you can choose the vectors \left \{ |0\succ, 1\succ \right \} as the basis of the vector space

In this way, any state vector |\psi\succ of a qubit is a linear combination of the basis vectors (which in the language of quantum mechanics is called quantum superposition state)

|\psi\succ=\alpha|0\succ+\beta|1\succ

Which defines the linear combination of the basis states of a qubit

The Hilbert space of each qubit is a complex space

This means that \alpha and \beta are complex numbers

Furthermore, they cannot take any value

They must comply that:

\left\|\alpha\right\|^{2}+\left\|\beta\right\|^{2}=1

That is, the state vector of a qubit |\psi\succ is normalized, (its norm is equal to 1)

Quantum gates

Quantum gates are linear transformations between state vectors |\psi\succ

But it is not useful to perform any linear transformation

Only transformations that do not change the norm of the vector can be considered |\psi\succ

Those that take a vector whose norm is 1 and convert it into another vector with norm equal to 1

These types of transformations are called Unitary Transformations, or in the language of quantum mechanics, Unitary Operators

Therefore, quantum gates are unitary operators

Unitary operators can be represented as matrices

The application of a unitary operator A to a state vector |b\succ, can be represented as a multiplication of a matrix of 2x2 by a column vector, resulting in another column vector

\begin{pmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{pmatrix} \cdot \begin{pmatrix} b_{1}\\ b_{2} \end{pmatrix} = \begin{pmatrix} c_{1}\\ c_{2} \end{pmatrix}

Quantum gates of a qubit in Qiskit

Qiskit is a tool created by IBM to develop software for quantum processors

It is implemented in the programming language Python, and available for the general public to start experimenting and testing our algorithms

Below I am going to represent the most used quantum gates that are applied to a qubit, as well as their matrix representation and how to implement it in Qiskit

To know how install Qiskit on your computer, you can review it in its documentation

Identity Door

The identity door represents the identity matrix and it is called that because it represents the identity map that goes from a finite dimensional vector space to itself

I=\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}

And whose representation in Dirac notation is:

\begin{matrix} I|0\succ=|0\succ\\ I|1\succ=|1\succ \end{matrix}

Qiskit code of the identity door:

Pauli matrices

Pauli matrices are named after Wolfgang Ernst Pauli

They are matrices used in quantum physics in the context of intrinsic angular momentum or spin

Mathematically, the Pauli matrices constitute a vector basis of the Lie algebra of the unitary special group SU\left(2\right), acting on dimension 2 representation

Gate X (Pauli X matrix)

I=\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}

And whose representation in Dirac notation is:

\begin{matrix} I|0\succ=|1\succ\\ I|1\succ=|0\succ \end{matrix}

Gate X Qiskit code:

Gate Y (Pauli Y matrix)

I=\begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}

And whose representation in Dirac notation is:

\begin{matrix} I|0\succ=i|1\succ\\ I|1\succ=-i|0\succ \end{matrix}

Gate Y Qiskit code:

Gate Z (Pauli Z matrix)

I=\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}

And whose representation in Dirac notation is:

\begin{matrix} I|0\succ=|0\succ\\ I|1\succ=|-1\succ \end{matrix}

Gate Z Qiskit Code:

Hadamard Gate

The Hadamard Gate, named after Jacques Salomon Hadamard

They are nothing more than the representation of a qubit of the quantum Fourier transform

Since the rows of the matrix are orthogonal, where {\displaystyle H} is a unitary matrix

H=\frac{1}{\sqrt{2}}\cdot \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}

And whose representation in Dirac notation is:

\begin{matrix} H|0\succ=\frac{1}{\sqrt{2}}\cdot \left(|0\succ+|1\succ\right)\\ H|1\succ=\frac{1}{\sqrt{2}}\cdot \left(|0\succ-|1\succ\right) \end{matrix}

Gate H Qiskit code:

Gates U (Universal)

In previous versions of qiskit there were so-called Gates U1, U2 and U3

But these doors have disappeared in later versions of qiskit

They can be built with P and U gates using the following equivalences:

\left\{\begin{matrix} U_{1}\left(\lambda\right)=P\left(\lambda\right)\\ U_{2}\left(\Phi,\lambda\right)=U\left(\Phi,\lambda,\frac{\pi}{2}\right)\\ U_{3}\left(\theta,\Phi,\lambda\right)=U\left(\theta,\Phi,\lambda\right) \end{matrix}\right.

It is defined as

U\left(\theta,\Phi,\lambda\right)= \begin{pmatrix} \cos{\frac{\theta}{2}} & -\exp^{i\cdot\lambda}\cdot\sin{\frac{\theta}{2}} \\ \exp^{i\cdot\Phi}\cdot\sin{\frac{\theta}{2}} & \exp^{i\cdot\left(\Phi+\lambda\right)}\cdot\cos{\frac{\theta}{2}} \end{pmatrix}

There are two specific cases in which we obtain the following equivalences:

\left\{\begin{matrix} H=U\left(\frac{\pi}{2},0,\pi\right)= \frac{1}{\sqrt{2}}\cdot \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\\ P=U\left(0,0,\lambda\right)= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\lambda} \end{pmatrix} \end{matrix}\right.

Gate P

P\left(\Phi\right)= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\Phi} \end{pmatrix}

Gate P Qiskit code:

Gate U1

U_{1}\left(\lambda\right)= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\lambda} \end{pmatrix} =P\left(\lambda\right)

Qiskit code to Gate U1 in previous versions:

Gate U2

U_{2}\left(\Phi,\lambda\right)= \frac{1}{\sqrt{2}}\cdot\begin{pmatrix} 1 & -\exp^{i\cdot\lambda} \\ \exp^{i\cdot\Phi} & \exp^{i\cdot\left(\Phi+\lambda\right)} \end{pmatrix} =U\left(\Phi,\lambda,\frac{\pi}{2}\right)

Qiskit code to Gate U2 in previous versions:

Gate U3

U_{3}\left(\theta,\Phi,\lambda\right)= \begin{pmatrix} \cos{\frac{\theta}{2}} & -\exp^{i\cdot\lambda}\cdot\sin{\frac{\theta}{2}} \\ \exp^{i\cdot\Phi}\cdot\sin{\frac{\theta}{2}} & \exp^{i\cdot\left(\Phi+\lambda\right)}\cdot\cos{\frac{\theta}{2}} \end{pmatrix} =U\left(\theta,\Phi,\lambda\right)

Qiskit code to Gate U3 in previous versions:

Gate S

Make a quarter turn around the Bloch sphere

It is defined as \Phi =\frac{\pi}{2} and for that reason it is equivalent to a Gate U_{1}\left(\frac{\pi}{2}\right)

S= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\frac{\pi}{2}} \end{pmatrix} = U_{1}\left(\frac{\pi}{2}\right)

And whose representation in Dirac notation is:

SS|q\succ=\mathbb{Z}|q\succ

Gate S Qiskit code:

Gate S

Arises from a negative Gate S

It is defined as \Phi =-\frac{\pi}{2} and for that reason it is equivalent to a Gate U_{1}\left(\frac{-pi}{2}\right)

S= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{-i\cdot\frac{\pi}{2}} \end{pmatrix} = U_{1}\left(\frac{-pi}{2}\right)

Qiskit code to Gate S:

Gate T

It is similar to the Gate S and is also known as the \sqrt[4]{\mathbb{Z}}

It is defined as \Phi =\frac{\pi}{4} and for that reason it is equivalent to a Gate U_{1}\left(\frac{\pi}{4}\right)

T= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\frac{\pi}{4}} \end{pmatrix} = U_{1}\cdot\left(\frac{\pi}{4}\right)

Qiskit code of Gate T:

Gate T

Arises from a negative Gate T

It is defined as \Phi =-\frac{\pi}{4} and for that reason it is equivalent to a Gate U_{1}\left(\frac{-\pi}{4}\right)

T= \begin{pmatrix} 1 & 0 \\ 0 & \exp^{i\cdot\frac{\pi}{4}} \end{pmatrix} = U_{1}\cdot\left(\frac{-\pi}{4}\right)

Qiskit code to Gate T:

Standard rotation gates

These gates represent a particular type of rotation, derived from Pauli matrices

In general they are defined as:

R_{p}\left(\theta\right)=\exp\left(-\frac{i\cdot\theta}{2}\right)=\cos\left(\frac{\theta}{2}\right)\cdot I-i\cdot \sin\left(\frac{\theta}{2}\right)\cdot P

Gate Rx (Standard rotation around the x-axis)

The Gate Rx it is equivalent to U\left(\theta,\frac{-\pi}{2},\frac{\pi}{2}\right)

R_{x}\left(\theta\right)= \begin{pmatrix} \cos\left(\frac{\theta}{2}\right) & -i\cdot\sin\left(\frac{\theta}{2}\right) \\ -i\cdot\sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{pmatrix} = U\left(\theta,\frac{-\pi}{2},\frac{\pi}{2}\right)

Qiskit code to Gate Rx:

Gate Ry (Standard rotation around the y-axis)

The Gate Ry it is equivalent to U\left(\theta,0,0\right)

R_{y}\left(\theta\right)= \begin{pmatrix} \cos\left(\frac{\theta}{2}\right) & -\sin\left(\frac{\theta}{2}\right) \\ \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{pmatrix} = U\left(\theta,0,0\right)

Qiskit code to Gate Ry:

Gate Rz (Standard rotation around the z-axis)

The Gate Rz it is equivalent to P\left(\phi\right)

R_{z}\left(\phi\right)= \begin{pmatrix} \exp^{-i\cdot \frac{\phi}{2}} & 0 \\ 0 & \exp^{i\cdot \frac{\phi}{2}}\end{pmatrix} = P\left(\phi\right)

Qiskit code to Gate Rz:

Programming language

Programming language

A programming language can be any artificial language that can be used to define a sequence of instructions for processing on a machine (industrial machinery, a computer, a tablet, a mobile)

It is generally assumed that the translation of the instructions into a code comprising the machine must be completely systematic. It is usually the machine that performs the translation

Low level language

At a very low level, microprocessors exclusively process binary electronic signals. Giving a microprocessor instruction actually involves sending series of a few and zeros spaced in time in a given way. This sequence of signals is called machine code

The code typically represents data and instructions for manipulating it. An easier way to understand machine code is by giving each statement a mnemonic, such as STORE, ADD, or JUMP. This abstraction results in the assembler, a very low-level language that is specific to each microprocessor

The low level languages allow you to create programs that are very fast, but difficult to understand. The most important thing is the fact that programs written in low level are practically specific to each processor. If you want to run the program on another machine with other technology, you will need to rewrite the program from scratch

High level language

Machines are generally thought to perform calculation or word processing tasks. The description above is just a very schematic way to see how they work. There is a high level of abstraction between what the machine is asked to do and what it really understands. There is also a complex relationship between high level languages and machine code

The high level languages are easier to understand than machine code because they are made up of elements of natural languages, such as English. In BASIC (one of the high-level languages best known for their simplicity of learning), commands such as 'IF COUNTER s 10 THEN STOP' can be used to ask the machine to stop if the COUNTER variable is equal to 10. Unfortunately for many people this way of working is a little frustrating, given that even though machines seem to understand natural language, they actually do so in a rigid and systematic way

Interpreters and compilers

Translating a series of assembly language statements (source code) into machine code (or object code) is not a very complicated process and is typically performed by a special program called a compiler. The translation of high level source code into machine code is also done with a compiler, in this case this program will be more complex, or by an interpreter

A compiler creates a list of machine code instructions, the object code, based on a source code. The resulting object code is an executable program that can already work on the machine, but that can cause it to crash if it is not well designed

The interpreters, on the other hand, they are slower than compilers because they do not produce an object code, but run through the source code one line at a time. Each line is translated into machine code and executed. When the line is read a second time, as in the case of programs where parts of the code are reused, it must be recompiled. Although this process is slower, it is less likely to cause unwanted locks on the machine

Assembler

Assembler

The assembly language is a low level language whose instructions usually correspond one by one to the instructions of the computer, and with the possibility to be able to define, for example, macroinstrucciones

This language is totally dependent on the processor model

To be able to study this language with a real microprocessor has been chosen the 8086, as it is one of the most used today on PC type computers

The main feature of that microprocessor is that it is capable of working with 16 bit operands

PC-type computers use the basic 16 bit operations of the 8086, and if the processor is 32 bit, a new repertoire of 32 bit operations is added to it. If it is 64 bit then the repertoire of operations included are 16 bit, 32 bit and a new 64 bit repertoire

Basic architecture of the 8086

The internal architecture of the 8086 consists of two distinct parts: the Execution Unit and the Interface Unit with the bus

The main task of the Execution Unit is to execute the statements it extracts from the queue, where they arrive from the MP (Main Memory)

The main job of the Interface Unit with the bus is to search and bring the MP instructions. To do this you will need to generate the address of the statement to be executed

General records or data

There are 4 registers of 16 bit and each one has a specific utility:

  • AX
    Accumulator record, used with arithmetic operations
  • BX
    Base record, used to access vectors, tables, etc
    Through the use of a base address that indicates which is the first element and a direction of displacement which is the value that is added to the base address in order to reach the desired item
  • CX
    Counter record, used to count in loops and repetitive type instructions
  • DX
    Data logging, has two particular uses

    1. Complement the AX register in the arithmetic operations of multiplication and division
    2. Contains port addresses for input/output instructions

These 4 records have the following common property, although they are 16 bits, each can be considered as 2 separate 8 bit records. In this way we can designate the highest or most significant bits as Hight (the record is renamed with H at the end: AH, BH, CH, DH) and the lowest or least significant ones as Lower (the record is renamed with L at the end: AL, BL, CL, DL)

Segment records: Segmentation

There are 4 registers of 16 bit and each one has a specific utility:

  • CS
    Code segment, is the one that defines the memory zone where the instructions to be executed or program are located
  • DS
    Data segment, is the one that defines the area where the data to be used in the program is located
  • SS
    Stack or stack segment, defines the memory zone to use as a stack
  • It IS
    Extra segment, as the name implies contains the address of the extra segment, which is used when the capacity of 64 kbytes of the data segment is exceeded and to perform certain data transfers between segments

Data Bus: lines that transmit the data

Data bus width: number of bus power lines through which data is transmitted, in 8086 they are 16 bit

Address Bus: lines that transmit the addresses

Address bus width: number of bus power lines through which addresses are transmitted, in 8086 they are 16 bit

Therefore, the addresses should be 16 bits, however, on the 8086 they are 20 bits. This is because the number of different addresses would be 2^{16}=64K, but because they are very few, we are interested in them being 20 bits, since 2^{10}=1M

To calculate the physical direction we need a segment and an offset, always using hexadecimal values, so we will have a first 16 bit record to which a 0 will be added to its right and a second 16 bit record

As the notation is used Segment:Displacement

The calculation of the physical address shall be carried out:
Segment x 10(h) + Offset

Examples of Segment:Displacement

  • 0000:1050\Rightarrow 0000\cdot 10+1050=0150
  • 0010:0F50\Rightarrow 0010\cdot 10+0F50=0150
  • 0100:0050\Rightarrow 0100\cdot 10+0050=0150
  • 0105:0000\Rightarrow 0105\cdot 10+0000=0150

As can be seen in the examples, different combinations of segments and displacements can give us the same physical direction. This situation is not an error, it is because the directions of the segments overlap each other

We can have as many different segment numbers as different combinations can be made with the segment base direction. Since it has 16 bits then we have 2^{16} combinations so that we have 64\cdot K different directions

We can have as many different address numbers in each segment as different combinations can be made with scrolling. Since it has 16 bits then we have 2^{16} combinations so that we have 64\cdot K different directions

You might think that as we have 64\cdot K segments and 64\cdot K addresses, we would have 64\cdot 64=2G physical addresses. However, because the addresses are overlapped this number of addresses is incorrect

Valid segment start addresses have the property of ending in 0. Therefore, since in hexadecimal they are 10, when moving from hexadecimal to decimal we get that the number of addresses is 16

SQL

SQL

To manage the data base data model most commonly used is the relational model and to communicate with the manager can use the SQL language

It is NOT a programming language, but a database management language that has the following characteristics:

  • DDL (Data Definition Language)

    The DDL SQL provides commands for defining relation schemas, deleting and changing relationships schemes relationship

  • DML (Data Manipulation Language)

    The DML SQL includes a query language based on both relational algebra and tuple relational calculation. It also includes commands to insert, delete, and modify tuples

  • Integrity

    The DDL includes commands to specify integrity constraints that must meet the data stored in the database. Updates that violate integrity restrictions are rejected

  • Definition of views

    The SQL DDL includes commands for defining views

  • Transaction control

    SQL includes commands to specify the start and end of transactions

  • Built-in SQL and dynamic SQL

    Built-in SQL and Dynamic SQL define how SQL statements can be incorporated into general-purpose programming languages such as C, C++, Java, PL/L, Cobol, Pascal or Fortran, among others

  • Authorization
    The SQL DDL includes commands to specify access rights to relationships and views

Evolution of SQL

IBM developed the original version of SQL, called Sequel, as part of the System R project in the early 1970s by the San Jose, California

The Sequel language has evolved since then and its name has become SQL (Structured Query Language)

Today, many programming languages support the SQL language and have been set as the standard language for the use of relational databases

1981, IBM released the first commercial SQL-based program

1986, ANSI (American National Standards Institute) published a SQL standard, called SQL-86 or SQL1 and was confirmed by ISO (International Standards Organization) in 1987

1989 ANSI released an extension of the standard for SQL named SQL-89

1992, a new version of the standard called SQL-92 or SQL2 appeared

1999, a new version called SQL:1999 or SQL2000 appeared. In which regular expressions, recursive queries (for hierarchical relationships), triggers, and some object-oriented features were added

2003, a new version called SQL:2003 appeared. In which some XML features, changes in functions, standardization of the sequence object and autonumeric columns were added

2005, a new version called SQL:2005 appeared and corresponds to the ISO/IEC 9075-14:2005 standard. Defining how SQL can be used in conjunction with XML:

  • Import and save XML data into a SQL database, manipulating it within the database and publishing conventional XML and SQL data in XML
  • Facilities that allow applications to integrate into their SQL code the use of XQuery, XML query language published by the World Wide Web Consortium (W3C) for concurrent access to ordinary SQL data and XML documents

2008, a new version called SQL:2008 appeared. In which the use of the ORDER BY clause was added outside the cursors definitions. Including triggers of the INSTEAD OF type. And the inclusion of the TRUNCATE judgment

2011, a new version called SQL:2011 appeared. In which the temporary data (PERIOD FOR) and improvements in the window functions and the FETCH clause were added

2016, a new version called SQL:2016 appeared. In which the search was added using patterns, polymorphic table functions and compatibility with JSON files

DDL

Data Definition Language (DDL)

Data Definition Language (DDL) is responsible for modifying the structure of database objects

Includes orders to modify, delete, or define relationships (tables) in which database data is stored

There are four basic operations:

Definition of data

The relationship set for each database must be specified in the system in terms of a data definition language (DDL)

The SQL DDL not only allows the specification of a set of relationships, but also information regarding those relationships, including:

  • The outline of each relationship
  • The value domain associated with each attribute
  • Integrity restrictions
  • The set of indexes that must be maintained for each relationship
  • The security and authorization information for each relationship
  • The physical storage structure of each relationship on disk

Basic types of domains

The SQL standard supports a wide variety of predefined domain types:

  • CHAR(n)

    A fixed length character string, with a length n specified by the user. You can also use the full word character

  • VARCHAR(n)

    A variable length character string with a maximum length n specified by the user. The complete form, character varying, is equivalent

  • INT

    An integer (a finite subset of computer dependent integers). The whole word, integer, is equivalent

  • SMALLINT

    A small integer (a computer dependent subset of the entire domain type)

  • NUMERIC(p, d)

    A fixed comma number, the accuracy of which is specified by the user. The number is made up of p digits (plus the sign) and those p digits, d belongs to the decimal part

  • DOUBLE

    A floating-point number and double precision floating point numbers, with computer dependent precision

  • FLOAT(n)

    A floating point number whose accuracy is, at least n digits

  • DATE

    Date type data, with an accuracy of up to second

Basic definition of schemes

CREATE TABLE

Relationships are defined by the command CREATE TABLE:

\text{CREATE TABLE r} (\\ A_1 D_1, \cdots, A_n D_n,\\ \{rest-integrity_1\}, \cdots, \{rest-integrity_k\} )

Where r is the name of relationship, every Ai is the name of an attribute in the relationship scheme r and Di is the domain type of the attribute domain values Ai

There are several valid integrity restrictions

One of the most commonly used is the primary key (primary key), which takes the following form:

\text{primary key}(A_{j1}, \cdots, A_{jm})

The primary key specification determines the attributes A_{j1}, \cdots, A_{jm} form the primary key to the relationship

The attributes of the primary key must be non null and unique; that is, no tuple can have a null value for an attribute of the primary key and no pair of tuples in the relationship can be equal across all attributes of the primary key

Although the primary key specification is optional, it is usually a good idea to specify a primary key for each relationship

The example created a relationship called acount in which the attributes have been defined:

number_acount, you can save a word of up to 10 characters
name_branch, you can save a word of up to 15 characters
balance, you can save a decimal number with 12 integers and 2 decimal places
And with the restriction on number_acount as the primary key to the relationship

If a newly inserted or newly modified tuple in a relationship contains null values for any of the attributes that are part of the primary key, or if they have the same value as another tuple in the relationship, SQL will report the error and prevent the update

The example created another relationship called client in which the attributes have been defined:

number_acount, you can save a word of up to 10 characters
name_client, you can save a word of up to 15 characters
And with the restriction on number_acount as the primary key to the relationship

As can be seen we can create more than one relationship

DROP TABLE

The command is used to delete a relationship from the database DROP TABLE

This command removes all information about the relationship from the database

The example has removed all information about the relationship from the database acount

This command is more drastic than DELETE, which only erases your tuples while maintaining the outline of the relationship

However, DROP, if removed acount, no tuples can be reinserted into that relationship unless it is recreated with the command CREATE TABLE

ALTER TABLE

The command ALTER TABLE is used to add attributes to an existing relationship

As the value of the new attribute, all tuples in the relationship are assigned null value

The example added a new attribute

Where acount is the name of the existing relationship, year is the name of the attribute you want to add and DATETIME is the domain of the added attribute

You can also remove attributes using ALTER TABLE

The example removed the attribute we added earlier

Where acount is the name of the existing relationship and year is the name of the attribute you want to delete

In this case it is not necessary to declare the domain of the attribute to be deleted

Many database systems do not allow the deletion of attributes, although they do allow the deletion of entire tables

TRUNCATE

This command applies only to tables and its function is to clear the entire contents of the specified relationship

The advantage over the command DELETE, is that if you want to erase all the content of the relationship, it is much faster, especially if the relationship is very large

The downside is that TRUNCATE only serves when you want to remove absolutely all tuples, as the clause is not allowed WHERE

While, at first, this statement would appear to be Data Manipulation Language (DML), it is actually a DDL, because internally, the TRUNCATE command clears the table and recreates it and does not execute any transactions

In the example we've deleted all the content of the called relationship acount

DML

Data Manipulation Language (DML)

Data Manipulation Language (DML) is responsible for carrying out the tasks of querying or manipulating the data, organized by the appropriate data model

Includes commands to query, modify, delete, or define tuples (rows) that are stored in the relationships (tables) of the database

There are four basic operations:

Basic definition of schemes

Alias Operation

SQL provides an operation to rename using an alias for both relationships and attributes

It has the following syntax: original_name AS alias

The clause AS it is optional although if we write it will be clearer because it informs us that we are defining an alias

Situations where it is useful to use an alias:

  • Where we have two relationships in the clause FROM who have the same name for their attributes

    In these situations there may be ambiguity and we can use the alias operation to disassemble the attributes and make it clear which relationship they belong to

  • When we have a calculated operation that generates an attribute in the clause SELECT

  • When we have a calculated operation that generates an attribute after applying a aggregation operation

To access its attributes, you must use the alias followed by a period and the attribute name

SELECT

You can use the command SELECT to query the data stored in a database relationship

It will return a set of tuples from that relationship

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1, r_2, \cdots, r_m\\ \text{WHERE} P

Where every Ai represents an attribute, each ri a relationship and P it's a predicate

So an SQL query will have the following equivalent relational algebra expression:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p(r_1 \Theta r_2 \Theta \cdots \Theta r_m) )

If the clause is omitted WHERE, the predicate P it's always True

But unlike relational algebra expression, the result of an SQL query can contain multiple repeated tuples

When we introduce more than one relationship into the aclausula FROM, SQL will form a Cartesian product of all included relationships

The selection of relational algebra is then carried out using the predicate of the WHERE and then projects the result on the attributes of the clause SELECT

SELECT clause

The example resolved the query to get all the accounts in the relationship acount with attributes number_acount, name_branch, balance

In this case, when using DISTINCT we are forcing the repeated to be removed

The example resolved the query to get all the accounts in the relationship acount with attributes number_acount, name_branch, balance

In this case, when using ALL we are forcing the repeated ones not to be eliminated

The example replaced the command ALL by the symbol * and the attributes have been dispensed with, as the result is equivalent

The example resolved the query to get all the accounts in the relationship acount with attributes number_acount, name_branch, with an increase of 1500 in the balance

As can be seen, we can also use arithmetic expressions that operate on attributes

The example created a calculated attribute (non relationship) from those operations, giving it a name or alias to that operation, in this case it's called increase

FROM clause

The clause FROM itself defines a Cartesian product of the relationships that appear in the consultation

Because natural meeting is defined in terms of a Cartesian product, a selection, and a projection, it is relatively easy to write an SQL expression for the natural meeting

We have to write it in relational algebra as:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (r_1 \Theta r_2 \Theta \cdots \Theta r_m)

The example resolves the query to get all customers who have an account at the 'Navacerrada

For this purpose, relationships have been introduced acount (you have been assigned the alias c) and client (you have been assigned the alias cl)

Only with the clause FROM we couldn't solve the query

Since the Cartesian product is made on each of the possible couples that can be formed with the relationship acount and client

Giving us repeated results and also all those who were not from the branch of 'Navacerrada

That's why the condition that relates both relationships, the attribute, has been added number_acount

After adding it, we will have only the values that belong to the relationship, although it can still have been repeated

And equality for the attribute name_branch with the value 'Navacerrada

JOIN Clause

The example used the clause JOIN that will give us back an equivalent result

The clause JOIN allows meeting operations, which take two relationships and return another relationship

The clause can normally be used FROM for external meetings or for any situation where a meeting can be used

To distinguish whether the meeting is external, the keyword will be used OUTER and for the intern INNER

Both are optional, although it makes it clearer what kind of meeting we're using

There are four types of meeting:

  • INNER JOIN

    Returns all tuples when there is at least one match in both relationships

    The example used the clause INNER JOIN that will give us back an equivalent result

    Because as we said, the keyword INNER was optional

    However, we now have clearer that the meeting was internal

  • LEFT OUTER JOIN

    Returns all tuples in the left relationship, and the matching tuples of the right relationship

    The example used the clause LEFT JOIN that will return a result that is no longer equivalent, as they will appear nulls values

    We've dispensed with the keyword OUTER because the result is equivalent

    In this case LEFT JOIN It keeps all tuples in the left relation (acount)

    The tuples of the right relationship (client) will be displayed if there is a match with those on the left

    If values exist in the left relationship but not in the right relationship, it will display null

  • RIGHT OUTER JOIN

    Returns all tuples in the relationship on the right, and the matching tuples of the relationship on the left

    The example used the clause RIGHT JOIN that will return a result that is no longer equivalent, as they will appear nulls values

    We've dispensed with the keyword OUTER because the result is equivalent

    In this case RIGHT JOIN keeps us all the tuples of the right relationship (client)

    The tuples of the left relationship (acount) will be displayed if there is a match with those on the right

    If there are values in the right relationship but not in the left relationship, it will display null

  • FULL OUTER JOIN

    Returns all tuples of the two relationships, left and right

    The example used the clause FULL JOIN that will return a result that is no longer equivalent, as they will appear nulls values

    We've dispensed with the keyword OUTER because the result is equivalent

    In this case FULL JOIN returns all rows in the left table (acount) and the right table (client)

    It combines the outcome of the LEFT JOIN and RIGHT JOIN

    Null will appear on each of the tables alternately when there is no match

The type of assembly defines how to treat tuples of each relation that do not marry the tuple of the other relation

There are three types of conditions that apply to the meetings:

  • meeting NATURAL

    It is used when the attributes which meet relationships have the same name

    The example used the clause NATURAL JOIN which will return an equivalent result without using the condition ON

    Since the attribute number_acount It called equal relations acount and client

  • condition ON

    Its use is similar to that of the clause WHERE, but it specializes in comparing attributes of both relations

  • condition USING

    Its use is similar to that of the meeting NATURAL, because it is used when the attributes which meet relationships have the same name

    However, only return a result of the meeting, not all as the meeting does NATURAL

    The example used condition USING with the attribute number_acount, which will return a result that is no longer equivalent

    It will only return only the first match of each pair of the relationship, not all

Using conditions is mandatory meeting at external meetings, but is optional on the internal (if omitted, a Cartesian product is obtained)

WHERE clause

In the example, the query get resolved only accounts of the relationship acount that meet the condition introduced in this case in its attribute balance have a value less than 100

To achieve this we have used the clause WHERE, thanks to which we can filter the query to admitting a condition using the logical comparison operator <

Logical comparison operators
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
= Like
<> Other than that

In the example, the query get resolved only accounts of the relationship acount that meet the condition introduced in this case in its attribute balance have a value equal to 'Navacerrada' and its attribute balance have a value less than 100

To achieve this we have used the logical connector AND

Logical connection operators
Operator Description
AND It's true if and only if the two operands are true
OR It's true if and only if the one or both operands are true
NOT Reverses the value of the operand
BETWEEN Operator

In the example, the query get resolved only accounts of the relationship acount that meet the condition introduced in this case in its attribute name_branch have a value equal to 'Navacerrada' and its attribute balance they have a value between 100 and 1000

The example used an equivalent query, using the comparison operator BETWEEN, which simplifies us some clauses WHERE

In the example it used a query negating the comparison operator NOT BETWEEN, which will return those values ​​outside that range

LIKE operator

In the example, the query get resolved only accounts of the relationship acount that meet the condition introduced in this case in its attribute name_branch have a value equal to 'Navacerrada

The example used an equivalent query, using the comparison operator LIKE, which simplifies us some clauses WHERE on allowing the use of patterns with strings

For the description of patterns we can use two special characters:

  • %

    The percentage matches any substring

  • _

    Underlining matches any character

These patterns can be combined to obtain different types of queries:

In the example it used the comparison operator LIKE using the pattern % after the text, which will return the values ​​that begin with the string 'Nava

In the example it used the comparison operator LIKE using the pattern % before and after the text, which will return values ​​that include as substring 'Nava

In the example it used the comparison operator LIKE using the pattern _ repeated three times, which will return values ​​that are exactly three characters

In the example it used the comparison operator LIKE using the pattern _ repeated three times and pattern %, Which will return to those values ​​that have the least three characters

In the example it used the comparison operator LIKE using the pattern % together with the special escape character '\' which will return those values ​​containing 'Nava%rrete

The escape character (which can be anything we choose) is used before special characters pattern to indicate that this character be treated as a normal character and not as a pattern

It is defined using the keyword escape

In the example it used the comparison operator NOT LIKE using the pattern %, which will return those values ​​that do not contain 'Nava%rrete

In this way we can also look for inconsistencies, but denying the operator LIKE

Nulls values

SQL allows the use of null values ​​to indicate the absence of information on the value of an attribute

You can use the special keyword NULL in a predicate to check if a value is null

In the example, the query get resolved all accounts whose name NO branch has been filled

The example resolved the query to get all accounts whose branch name has been filled in

As can be seen, it can also be checked the absence of nulls using the logical operator NOT

Since the predicate clauses WHERE may include Boolean operations such as AND, OR and NOT on the results of the comparisons, values NULL Boolean operations expand

AND TRUE FALSE NULL
TRUE TRUE FALSE NULL
FALSE FALSE FALSE FALSE
NULL NULL FALSE NULL
OR TRUE FALSE NULL
TRUE TRUE TRUE TRUE
FALSE TRUE FALSE NULL
NULL TRUE NULL NULL
NOT TRUE FALSE NULL
FALSE TRUE NULL

ORDER BY clause

Sometimes we need to return the information of the tuples in a certain order

SQL allows you to sort the attributes by using the clause ORDER BY

In the example it used the comparison operator ASC with clause ORDER BY for the attribute name_branch, Which will return the values ​​sorted in ascending order (in alphabetical order because it was an alphanumeric attribute) by the attribute name_branch

In the example it used the comparison operator DESC with clause ORDER BY for the attribute name_branch, Which will return the values ​​sorted in descending order (in alphabetical order because it was an alphanumeric attribute) by the attribute name_branch

The sort type returned by the comparison operators ASC and DESC depend on the type of the attribute because not ordered in the same way a number, a string or a date

If you need to order more than one attribute, we'll separated by commas and followed the comparison operator (ASC or DESC )

The default sorting method is ASC, is optional and could do without writing

Operations on sets

SQL allows operations on sets of relational algebra union \cup, intersection \cap and difference \neg

To use them we will have the only restriction that the names of the attributes of the relationships involved must be equal

UNION operation

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{UNION}\\ \text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a union will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \cup \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

The example query resolved all account numbers that exist in the bank or have a customer associated with them

Unlike the clause SELECT the operation UNION eliminates duplicate values

For this reason it is good idea to use operation UNION to create keys, because with SELECT would not get unique keys (allows repeated)

The example query resolved all account numbers that exist in the bank or have a customer associated with them

In the use operator ALL in conjunction with the operation UNION, we are allowing it to behave like a SELECT, as it supports repeated

INTERSECT operation

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{INTERSECT}\\ \text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains an intersection will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \cap \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

The example query resolved all account numbers that exist in the bank and have a customer associated with them

The operation INTERSECT eliminates duplicate values ​​and returns only those values ​​that match the relationship acount and in client at once, not just one of them

The example query resolved all account numbers that exist in the bank and have a customer associated with them

In the use operator ALL in conjunction with the operation INTERSECT, we are allowing it to behave like a SELECT, as it supports repeated

EXCEPT operation

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{EXCEPT}\\ \text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a difference will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \cap \neg (\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) ) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

The example query resolved all account numbers that exist in the bank and do not have a customer associated with them

The operation EXCEPT eliminates duplicate values ​​and returns only those values ​​that match the relationship acount but do not match client

The example query resolved all account numbers that exist in the bank and do not have a customer associated with them

In the use operator ALL in conjunction with the operation EXCEPT, we are allowing it to behave like a SELECT, as it supports repeated

IN operation

SQL lets you check the membership of tuples to a relation which is equivalent to check the membership of a set of relational algebra

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{IN}\\ (\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q)

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a membership will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \in \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

The example resolves the query to get all customers who have an account at the 'Navacerrada

Previously we solved it using the clause JOIN

But as you can see, we can also solve it using set theory

Membership will return the values and if there are none, it will show null

Resolves example query to get all customers who do not have accounts on the branch 'Navacerrada

To deny membership will return all other values ​​and should be none, will show null

The example resolves the query to get all customers who have an account at the 'Logroño‘ ó ‘Navacerrada

As can be seen, belonging or not belonging apart from a subquery as in the previous examples, also it supports sets listed

Whenever these sets are listed in the same domain as the attribute that you want to check membership

Comparison operators

SQL provides the ability to compare sets using subqueries

SOME operator

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{SOME}\\ (\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q)

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a membership will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \approx \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

Example resolves the query to get a client who has an account on the branch 'Navacerrada

Previously we solved using the operation IN

But as can be seen, we can also solve using a comparison of sets

In this case using the symbol = in front of the operator SOME to denote that they are equal

Example resolves the query to get a client who does not have an account at the branch 'Navacerrada

Although it might seem that is equivalent to the previous example NOT INT, the result may not be the same

We can also use the rest of the logical comparison operators

ANY operator

The operator can also be used ANY since it is equivalent to SOME

Early versions of SQL only allowed the operator ANY, but was added SOME to be more asemejase to natural language, as created any ambiguity in the English language

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{ANY}\\ (\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q)

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a membership will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \approx \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

We can also use the rest of the logical comparison operators

Example resolves the query to get a client who has an account on the branch 'Navacerrada

Previously we solved using the operation SOME

As seen is fully equivalent and would use only for compatibility with older versions of SQL

ALL operator

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE} P\\ \text{ALL}\\ (\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} Q)

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query that contains a membership will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_1) ) \forall \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (r_2) )

If the clause is omitted WHERE, the predicate P or Q (It depends on what is omitted), it is always true

We can also use the rest of the logical comparison operators

Resolves example query to get all customers who have accounts in the following branches'Navacerrada' (alphabetical)

Actually the use the symbol > in front of the operator ALL we are denoting that we look for all values that have an alphabetical order greater than 'Navacerrada

Repeated control

SQL lets you control whether a subquery has repeated tuples in its result

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_1\\ \text{WHERE UNIQUE}\\ (\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2, \cdots, r_n\\ \text{WHERE} P)

Where every Ai represents an attribute, each ri a relationship and P is preached

So a SQL query that contains a membership will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma \exists \prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r_2, \cdots, r_n) ) (r_1) )

If the clause is omitted WHERE, the predicate P, es siempre cierto

Formally assessment UNIQUE on a relationship is defined as false if and only if the ratio of two tuples ri and rk such that rik

Like checking ri = rk it is false if any attribute ri or rk is zero, it is possible that the outcome of UNIQUE is true even if several duplicates of the same tuple

Provided that at least one of the attributes of the tuple is null

Resolves example query to get all customers who have at most one account at the branch 'Navacerrada

This has been introduced relations account (has been assigned the alias c) and client (has been assigned the alias cl)

Only with the clause FROM we couldn't solve the query

We could have used the clause JOIN, although there may still be repeated

Why it has added the operator UNIQUE relating the two relationships with the attribute number_acount and equality for the attribute name_branch with the value 'Navacerrada

After adding them, we only values ​​pertaining to the relationship

Resolves example query to get all customers who have more than one account at the branch office 'Navacerrada

As can be seen, the denial of UNIQUE we can check more than one repeated

Aggregation functions

The aggregation functions are functions that take a collection (a set or multiset) value as input and return a single value

SQL allows for five aggregation functions:

  • AVG

    Allows the arithmetic mean of the collection, which must be numeric

    If there are nulls values at the entrance, they are ignored

    In the example query is resolved to obtain the arithmetic mean of the balance of all accounts

    It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called average

  • MIN

    It allows determine minimum value of the collection, which is not required to be numerical

    If there are nulls values at the entrance, they are ignored

    In the example query is resolved to obtain the lowest balance of all accounts

    It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called less

  • MAX

    It allows determine the maximum value of the collection, which is not required to be numerical

    If there are nulls values at the entrance, they are ignored

    In the example query is resolved to obtain the highest balance of all accounts

    It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called greater

  • SUM

    Allows the sum of the collection, which must be numeric

    If there are nulls values at the entrance, they are ignored

    In the example query is resolved to obtain the total balance of all accounts

    It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called liquid

  • COUNT

    Counts the number of elements that make up the collection, which is not required to be numerical

    If there are nulls values at the entrance, they are taken as 0

    In the example, the query get resolved many accounts are there altogether

    It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called total

GROUP BY clause

There are circumstances in which it would be desirable to apply the aggregation functions not only to a single set of tuples, but also a group of sets of tuples

In SQL we can specify by clause GROUP BY

The most common form of consultation will be:

\text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r\\ \text{WHERE} P\\ \text{GROUP BY} A_1, \cdots, A_2, \cdots, A_n\\ \text{HAVING} Q

Where every Ai represents an attribute, each ri a relationship and P, Q are predicates

So a SQL query containing a group will have the following equivalent expression of relational algebra:

\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma q (\prod_{A_1, \cdots, A_2, \cdots, A_n} (\sigma p (r) ) )

If the provisions are omitted WHERE or HAVING, the predicate P or Q (It depends on what is omitted), it is always true

In the example, the query get resolved there are many accounts in total for each branch

It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called total

In order for the grouping operation can be performed without errors we use the same order of attributes in both the clause SELECT as in GROUP BY

In case you are not going to intervene in the group, we can omit these attributes placing them on the right of those that will be used

HAVING clause

Sometimes we need a condition that applies to groups rather than tuples

To express such queries use the clause HAVING

In the example, the query get resolved there are many accounts in total branch 'Navacerrada

It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called total

It has been applied on the attribute condition name_branch with the value 'Navacerrada' using clause HAVING to apply on the grouping and not on the relationship

In the event that a clause would WHERE and one HAVING:

  1. SQL first apply the predicate clause WHERE
  2. The result will be divided into groups that satisfy the predicate clause HAVING

In the example, the query get resolved there are many accounts in total branch 'Navacerrada' and have a balance greater than 100

It was created a calculated attribute (not belonging to the relation) from these operations, assigning a name or alias to that operation, in this case it's called total

It has been applied on the attribute condition name_branch with the value 'Navacerrada

Additionally the condition applied on the attribute balance applying the grouping operation COUNT with the proviso that they are older than 100

Both have been on the clause aplicacadas HAVING

As can be seen, they can be applied without problems aggregation operations within clause HAVING but also they appear in clause SELECT

INSERT

Newly created relationships are initially empty

You can use the command INSERT to add data to the relationship

The most common way to insert query will be:

\text{INSERT INTO r}\\ \text{VALUES} (A_1, A_2, \cdots, A_n)

Where every Ai It represents an attribute, r relationship and each Pi it's a predicate

The example has solved the query to add the C-9732 has the branch 'Navacerrada' with a balance of 1200 €

The values are specified in the order in which the corresponding attributes are related in the relationship schema

The example has changed the order in which the corresponding attributes are related in the relationship schema and that is why the data has been entered in a mandatory manner, in the new order that we have specified

In this way we can choose in what order we enter the data

In the case of not introducing an attribute, SQL will try to enter a default or null value and if the primary key, notify the error and prevent the update

In the example, the query has decided to add the client 'Pedro Almodovar' for all account numbers that exist in the branch'Navacerrada' and they do not have a client associated with them

Instead of specifying a tuple it has been used as a subquery clause SELECT will allow us to specify a set of tuples

The most common form of subquery for insertion will be:

\text{INSERT INTO} r_1\\ \text{SELECT} A_1, \cdots, A_2, \cdots, A_n\\ \text{FROM} r_2\\ \text{WHERE} P

Where every Ai It represents an attribute, ri relationships and P it's a predicate

The clause is evaluated first SELECT and are inserted in the relation client tuples obtained from the subquery

We must be careful to the nested subqueries use, since abusing them, could create an infinite loop subqueries and insertion would be made infinitely, being locked data base

Luckily management systems databases have mechanisms or tools for mass insertion in a controlled manner avoiding blocking of the database

UPDATE

You can use the command UPDATE to modify the values ​​of a set of tuples existing relationship

The most common form of update query is:

\text{UPDATE r}\\ \text{SET} A_1 = P_1, A_2 = P_2, \cdots, A_n = P_n

Where every Ai It represents an attribute, r relationship and each Pi it's a predicate

In the example it has been updated relationship called acount, in particular to the attribute balance we have changed their value 100

The command will update all tuples of the relationship acount

In the example they have been updated only tuples that satisfy the condition introduced in this case in its attribute number_acount have a value equal to 'C-9732

Case operator

The order of two instructions UPDATE It is important because if the order of the upgrade instructions changes we can get inconsistent data

To solve this problem we have the operator CASE

The most common form of update query is:

\text{UPDATE r}\\ \text{SET} A_1 = P_1, A_2 = P_2, \cdots, A_n = P_n

Where every Ai It represents an attribute, r relationship and each Pi is a predicate which can be replaced by the following predicate Q:

\text{CASE}\\ \text{WHEN} P_1 \text{THEN} r_1\\ \text{WHEN} P_2 \text{THEN} r_2\\ \cdots\\ \text{WHEN} P_n \text{THEN} r_n\\ \text{ELSE} r_0\\ \text{END}

Where every Pi each predicate can be satisfied, each ri the result to be used in updating

If not met any of the predicates, the result would apply r0

In the example it has been resolved update query to add 0.05 to the balance when it is less than or equal to 100 with a modification of 0.05, when it is equal to 500 with a change of 0.07 when it is greater than 1000 with a modification of 0.25 and a change of 0.01 in any other case

Clause has been used CASE to solve for the equate with the attribute balance which it was the value update

DELETE

You can use the command DELETE to erase tuples from a relationship

The example has deleted the relationship called acount

The command will clear all tuples from the relationship acount

And only in this relationship, because it is not able to work on several

The most common form of consultation will be deleted:

\text{DELETE FROM r}\\ \text{WHERE} P

Where r It represents the relationship and each P it's a predicate

In the example, only tuples that meet the introduced condition have been deleted, in this case those in their attribute balance have a value less than 100

It should be borne in mind that the command DELETE first check each tuple of the relation acount and then erase all tuples that meet the condition

It is important to perform all checks before erasing since the order in which tuples are processed can vary the erase operation