<jupyter_start><jupyter_text>prepared by <NAME> (QLatvia)                 This cell contains some macros. If there is a problem with displaying mathematical formulas, please run this cell to load these macros. $ \newcommand{\bra}[1]{\langle 1|} $$ \newcommand{\ket}[1]{|1\rangle} $$ \newcommand{\braket}[2]{\langle 1|2\rangle} $$ \newcommand{\dot}[2]{ 1 \cdot 2} $$ \newcommand{\biginner}[2]{\left\langle 1,2\right\rangle} $$ \newcommand{\mymatrix}[2]{\left( \begin{array}{1} 2\end{array} \right)} $$ \newcommand{\myvector}[1]{\mymatrix{c}{1}} $$ \newcommand{\myrvector}[1]{\mymatrix{r}{1}} $$ \newcommand{\mypar}[1]{\left( 1 \right)} $$ \newcommand{\mybigpar}[1]{ \Big( 1 \Big)} $$ \newcommand{\sqrttwo}{\frac{1}{\sqrt{2}}} $$ \newcommand{\dsqrttwo}{\dfrac{1}{\sqrt{2}}} $$ \newcommand{\onehalf}{\frac{1}{2}} $$ \newcommand{\donehalf}{\dfrac{1}{2}} $$ \newcommand{\hadamard}{ \mymatrix{rr}{ \sqrttwo & \sqrttwo \\ \sqrttwo & -\sqrttwo }} $$ \newcommand{\vzero}{\myvector{1\\0}} $$ \newcommand{\vone}{\myvector{0\\1}} $$ \newcommand{\stateplus}{\myvector{ \sqrttwo \\  \sqrttwo } } $$ \newcommand{\stateminus}{ \myrvector{ \sqrttwo \\ -\sqrttwo } } $$ \newcommand{\myarray}[2]{ \begin{array}{1}2\end{array}} $$ \newcommand{\X}{ \mymatrix{cc}{0 & 1 \\ 1 & 0}  } $$ \newcommand{\Z}{ \mymatrix{rr}{1 & 0 \\ 0 & -1}  } $$ \newcommand{\Htwo}{ \mymatrix{rrrr}{ \frac{1}{2} & \frac{1}{2} & \frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} & \frac{1}{2} } } $$ \newcommand{\CNOT}{ \mymatrix{cccc}{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0} } $$ \newcommand{\norm}[1]{ \left\lVert 1 \right\rVert } $$ \newcommand{\pstate}[1]{ \lceil \mspace{-1mu} 1 \mspace{-1.5mu} \rfloor } $  Basics of complex numbers Complex numbers are used in mathematics and physics (including electrical engineering and quantum mechanics). In fact, in the mathematical sciences complex numbers are just as "real" as the real numbers, and are fundamental in many aspects of the scientific description of the natural world. When we describe a quantum system, each quantum state has an amplitude (the amplitude is equal to zero if the state is not observed). Amplitudes of quantum states can be complex values, therefore, we need to understand the basics of complex numbers to see how complex amplitudes affect quantum computation. Some people state that in quantum mechanics, complex numbers are unavoidable and carry the entire essence of physics. The structure of complex numbersComplex numbers are the generalization of real numbers. A complex number can be expressed in the form $a + bi$, where $a$ and $b$ are real numbers. The number $i$ is a solution to the equation $x^2+1=0$, and is called an imaginary unit. From this fact we can conclude, that $i^2 = -1$, and it can also be assumed that $i = \sqrt{-1}$.Suppose that we have a complex number $a + bi$. For this complex number, $a$ is called the real part, and $b$ is called the imaginary part. If $b=0$, then we have a real number $a$. If $a=0$, then we have a pure imaginary number $bi$. In the specific case when $a=b=0$, we have number zero. The set of complex numbers is denoted by $\mathbb{C}$. Below you can check how real numbers correspond to complex numbers.Here are few examples of complex numbers: $2+3i$, $5-2i$, $7i$. Note that $1i$ can be also written like $i$. Basic operationsLet's check how to do basic mathematical operations with complex numbers. Suppose that we have two complex numbers: $a+bi$, $c+di$. Note how we group the real and imaginary parts.Addition: $(a+bi) + (c+di) = (a+c) + (b+d)i$.Subtraction: $(a+bi) - (c+di) = (a-c) + (b-d)i$. Subtracting the number $(c+di)$ is the same as adding the number $-(c+di) = -c-di$.Multiplication: $(a+bi) \cdot (c+di) = ac + adi + bci + bdi^2 = ac + adi + bci - bd = (ac-bd)+(ad+bc)i$.Absolute value: $\mathopen|a+bi\mathclose| = \sqrt{a^2+b^2}$.Conjugate value: For a complex number $z = a+bi$, $\overline{z} = \overline{a+bi} = a-bi$ denotes its conjugate.Division: To divide $a+bi$ by $c+di$, we will use the conjugate values. $\frac{a+bi}{c+di} = \frac{(a+bi)\overline{(c+di)}}{(c+di)\overline{(c+di)}} = \frac{(a+bi)(c-di)}{(c+di)(c-di)} = \frac{(ac+bd) + (bc-ad)i}{c^2+d^2}$. Examples$(1+2i) + (3+4i) = (1+3) + (2+4)i = 4+6i$.$(1+2i) - (3+4i) = (1-3) + (2-4)i = -2-2i$.$(1+2i) \cdot (3+4i) = 1 \cdot 3 + 1 \cdot 4i + 2 \cdot 3i + 2 \cdot 4i^2 = 3 + 4i + 6i - 8 = (3-8)+(4+6)i = -5 + 10i$.$\mathopen|1+2i\mathclose| = \sqrt{1^2+2^2} = \sqrt{5}$.$z = 1+2i$ => $\overline{z} = 1-2i$.$\frac{1+2i}{3+4i} = \frac{(1 \cdot 3 + 2 \cdot 4) + (2 \cdot 3 - 1 \cdot 4)i}{3^2+4^2} = \frac{11+2i}{25} = 0.44 + 0.08i$.  Task 1 Calculate the following values on the paper.$(3-2i) + (7+i)$;$(4+5i) - (2+2i)$;$(2+i) \cdot (8-4i)$;$\mathopen|4+3i\mathclose|$;$\overline{5-6i}$. click for our solution Geometric representationSince a complex number is representable by two real numbers, it is possible to represent any complex number in two-dimensional plane. There also exists other notation of complex numbers: $a+bi = (a,b)$. Let's check how it looks like on two-dimensional plane. Horizontal axis is usually used to represent the real part of a complex number, while vertical axis is used to represent the imaginary part of a complex number. Therefore, the number $(a,b)$ can be represented by a vector on a complex plane pointing at $a$ coordinate on x-axis and at $b$ coordinate on y-axis. On the image below you can see a representation of the number $1+2i$ and its conjugate $1-2i$.The absolute value of the complex number is equal to the length of the vector, that represents it. If we have a real number, then its representing vector will be on x-axis, while pure imaginary number will be represented by a vector on y-axis. Conjugate of a number is a reflection over x-axis. Polar formThe ordinary form $a+bi$ for a complex number is also called the rectangular form. Now we will check another widely used representation of a complex number, inspired by geometrical representation. This form reminds the reasoning behind the representation of the real-value qubit on two dimensions. Our complex number can be defined by the length of its vector representation and the angle between the vector and x-axis counterclockwise. Let's check how it looks like. On the image below you can see the complex number $3+3i$. In case of this complex number, its angle - which is also called phase or argument - is $\alpha = \frac{\pi}{4}$, and length is $\sqrt{3^2+3^2} = 3\sqrt{2}$. As you can see, the coordinates of this number are related to its length and angle, and so it can be written as $(r \cdot \cos{\alpha}) + (r \cdot \sin{\alpha})i = r(\cos{\alpha} + \sin{\alpha} \cdot i)$. And now very interesting and important alternative form:$$\cos{\alpha} + \sin{\alpha} \cdot i = e^{i\alpha}$$.Therefore, any complex number can also be represented in the following form: $r \cdot e^{i\alpha}$. This is also called Euler form of a complex number.Let's check when this representation makes life much easier. Here we will check the multiplication of two complex numbers.\begin{align*}r(\cos{\alpha} + \sin{\alpha} \cdot i) \cdot s(\cos{\beta} + \sin{\beta} \cdot i) &= r \cdot s (\cos{\alpha} + \sin{\alpha} \cdot i)(\cos{\beta} + \sin{\beta} \cdot i) \\&= r \cdot s ((\cos{\alpha} \cos{\beta} - \sin{\alpha} \sin{\beta}) +(\cos{\alpha} \sin{\beta} + \sin{\alpha} \cos{\beta})i)\\&= r \cdot s (\cos{(\alpha + \beta)} + \sin{(\alpha + \beta)}i).\end{align*}In Euler form: $r \cdot e^{i\alpha} \cdot s \cdot e^{i\beta} = r \cdot s \cdot e^{i(\alpha + \beta)}$.By reasoning in a similar way, we can check another useful example:$(r \cdot e^{i\alpha})^k = r^k \cdot e^{i(k\alpha)}$.Now you will not fear matrices like this one:$ \mymatrix{cc}{ 1 & 0 \\ 0 & e^{i \frac{pi}{4}} } $.  Task 2 Convert $4-4i$ to polar form.Convert $2 \cdot e^{i \pi}$ from polar form to the rectangular form. click for our solution  Task 3 Recreate famous Euler formula. Please use:    =, + or -;    one instance of each of the following constants: 0, 1, $i$, $\pi$, $e$. click for our solution Complex numbers in PythonThere are two basic ways how to initialize a complex number in Python. First way - to explicitly create an imaginary number. For imaginary number $bi$ (where $b$ is real number), you need to write in Python $bj$. Therefore, in Python $j$ represents the imaginary unit $i$. The reason for this is that Python follows the electrical engineering convention, where $i$ is regularly used to represent a current. Just keep in mind that from now on we will use and see $j$ in our code and program outputs to represent the imaginary unit.Another way to initialize a complex number is to define it with the function _complex_: the number $a+bi$ is initialized by `complex(a,b)`.Let's check few examples.<jupyter_code>z1 = 3+2j
print(z1)
z2 = complex(3,2)
print(z2)
z3 = 5j
print(z3)
z4 = complex(0,5)
print(z4)<jupyter_output><empty_output><jupyter_text>We can use basic mathematical operations in our code.<jupyter_code>z1 = 3+2j
z2 = 4+5j
print('z1=',z1,'z2=',z2)
print('z1+z2=',z1+z2)
print('z1-z2=',z1-z2)
print('z1*z2=',z1*z2)
print('z1/z2=',z1/z2)<jupyter_output><empty_output><jupyter_text>Python has also many useful methods to work with complex numbers. Some standard methods also support complex numbers.<jupyter_code>z = 3+2j
print('real part of 3+2j:',z.real)
print('imaginary part of 3+2j:',z.imag)
print('conjugate of 3+2j:',z.conjugate())
print('absolute value of 3+2j:',abs(z))
print('(3+2j)^3:',pow(z,3))<jupyter_output><empty_output><jupyter_text>Task 4 Please calculate the following values in Python:    $\frac{3+4i}{2-i} \cdot (7+2i) + 3i$;    $(1-2i)^5$;    $\frac{8-4i}{\mathopen|4+3i\mathclose|}$.<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output><jupyter_text>click for our solution  Task 5 Use Python to convert:    $3 - 3i$ to polar form;    $5 \cdot e^{i \cdot \frac{pi}{3}}$ from polar form.<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16558121176238405, 'fraction_numerical': 0.031280836787053484, 'mean_word_length': 4.2215352910870685, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '68477', 'license': 'Apache-2.0', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3302, 'n_words': 1274, 'path': 'silver/C01_Complex_Number_Basics.ipynb', 'repo_name': 'dev-aditya/QWorld_Summer_School_2021', 'size': '10145'}