Exemple #1
0
def test_radd():
    """Test to make sure the __radd__ function works."""
    complexNumber = Complex(0, 3)
    assert (complexNumber + 3) == Complex(3, 3), error

    complexNumber = Complex(-7, -2)
    assert (complexNumber + 5) == Complex(-2, -2), error
Exemple #2
0
	def div(self, a, b):
		if isinstance(a, float):
			if isinstance(b, float):
				if b == 0:
					raise ComputorException('Division by zero')
				return a / b
			elif isinstance(b, Complex):
				return Complex.div(Complex(a), b)
			elif isinstance(b, Matrix):
				raise ComputorException('Illegal operation: Rational / Matrix')
		elif isinstance(a, Complex):
			if isinstance(b, float):
				return Complex.div(a, Complex(b))
			elif isinstance(b, Complex):
				return Complex.div(a, b)
			elif isinstance(b, Matrix):
				raise ComputorException('Illegal operation: Complex / Matrix')
		elif isinstance(a, Matrix):
			if isinstance(b, float):
				if b == 0:
					raise ComputorException('Division by zero')
				return Matrix.scalar_mul(1 / b, a)
			elif isinstance(b, Complex):
				raise ComputorException('Illegal operation: Matrix / Complex')
			elif isinstance(b, Matrix):
				return Matrix.mat_mul(a, b.get_inverse())
		raise ComputorException('Computor.div(): something bad happened 🤷')
Exemple #3
0
    def test_adding_negatives(self):
        result = Complex(-10, -20) + Complex(-1, -2)

        self.assertEqual(
            str(result), "-11.00 - 22.00i",
            "Adding two negative complex numbers didn't print the correct value"
        )
Exemple #4
0
    def test_adding_negative_to_positive(self):
        result = Complex(10, 20) + Complex(-1, -2)

        self.assertEqual(
            str(result), "9.00 + 18.00i",
            "Adding a negative complex to a positive complex number didn't print the correct value"
        )
Exemple #5
0
    def test_adding_positives(self):
        result = Complex(10, 20) + Complex(1, 2)

        self.assertEqual(
            str(result), "11.00 + 22.00i",
            "Adding two positive complex numbers didn't print the correct value"
        )
Exemple #6
0
def test_rsub():
    """Test to make sure the __rsub__ function works."""
    complexNumber = Complex(2, 0)
    assert (complexNumber - 3) == Complex(-1, 0), error

    complexNumber = Complex(-4, -2)
    assert (complexNumber - 3) == Complex(-7, -2), error
Exemple #7
0
 def test_sqrt(self):
     
     a = Complex(3,2)
     a = sqrt(a)
     b = complex(3,2)
     b = cmath.sqrt(b)
     self.assertEqual(a.re,b.real)
     self.assertEqual(a.im,b.imag)
     
     a = Complex(-3,-2)
     a = sqrt(a)
     b = complex(-3,-2)
     b = cmath.sqrt(b)
     self.assertEqual(a.re,b.real)
     self.assertEqual(a.im,b.imag)
     
     a = Complex(3,0)
     a = sqrt(a)
     b = complex(3,0)
     b = cmath.sqrt(b)
     self.assertEqual(a.re,b.real)
     self.assertEqual(a.im,b.imag)
     
     a = Complex(0,3)
     a = sqrt(a)
     a.re = round(a.re,2)
     a.im = round(a.im,2)
     b = complex(0,3)
     b = cmath.sqrt(b)
     breal = round(b.real,2)
     bimag = round(b.imag,2)
     self.assertEqual(a.re,breal)
     self.assertEqual(a.im,bimag)
Exemple #8
0
    def test_divide_with_zero_throws_ZeroDivisionError(self):
        self.assertRaises(ZeroDivisionError,
                          Complex(1, 2).divide, Complex(0, 3))


#if __name__ == '__main__':
#    unittest.main()
Exemple #9
0
    def test_divide_positives(self):
        result = Complex(1, 2) / Complex(2, 3)

        self.assertEqual(
            str(result), "0.50 + 0.67i",
            "Dividing a negative complex to a positive complex number didn't print the correct value"
        )
Exemple #10
0
def Qubit_after_measure(qubit):
    if P0(qubit) > P1(qubit):
        alfa = qubit.alpha / abs(qubit.alpha)
        return Qubit(alfa, Complex(0, 0))
    else:
        beta = qubit.beta / abs(qubit.beta)
        return Qubit(Complex(0, 0), beta)
Exemple #11
0
    def test_multiply_positives(self):
        result = Complex(1.0, 2.0) * Complex(2.0, 3.0)

        self.assertEqual(
            str(result), "2.00 + 6.00i",
            "Multiplying a negative complex to a positive complex number didn't print the correct value"
        )
def test_conjugate_complex():
    """Tests that the conjugate function in complex.py works
	
	Loops through the first list of z values and appends
	the result of the conjugation to a new list, then compares
	that list of computed values to a list of correct answers.
	"""

    # Hand-calculated values to test against
    z_real_list = [(-6, -3), (-3, 6), (0, -2), (3, 3), (7, 0)]

    z_computed_list = []
    for i in range(val_len):
        if isinstance(z1_values[i], (float, int)):
            z = Complex(z1_values[i])
        else:
            z = Complex(z1_values[i][0], z1_values[i][1])

        z_computed = z.conjugate()
        z_computed_list.append(z_computed())

    for i in range(val_len):
        msg = "\nError in value number %d\n\
				z1 = %s \n\
				z_real = %s \n\
				z_computed = %s"      \
            % (i, z1_values[i], z_real_list[i], z_computed_list[i])
        msg = msg.replace('	', '')

        assert z_real_list[i] == z_computed_list[i], msg
def test_rmul():
    """
    Tests multiplication starting with a Python built-in operation
    combined with an operation from our custom Complex class.
    Both are complex numbers.
    """
    assert ((2 + 3j) * (Complex(1, 2))) == Complex(2, 6)
def test_custom_to_python():
    """
    Tests operation between our Complex class and Python's
    build-in complex.
    Both are complex numbers.
    """
    assert (Complex(2, 3) + (2 + 2j)) == Complex(4, 5)
def test_radd():
    """
    Tests addition starting with a Python built-in operation
    combined with an operation from our custom Complex class
    Both are complex numbers.
    """
    assert ((2 + 2j) + (Complex(2, 3))) == Complex(4, 5)
Exemple #16
0
    def test_subract_positives(self):
        result = Complex(1, 2) - Complex(2, 3)

        self.assertEqual(
            str(result), "-1.00 - 1.00i",
            "Adding a negative complex to a positive complex number didn't print the correct value"
        )
Exemple #17
0
def test_equals():
    """Raises AssertionError if checking the equality of Complex objects does
    not return expected values.
    """
    z = Complex(11, -7)
    w = Complex(11, -7)
    assert z == w
def test_rsub():
    """
    Tests subtraction starting with a Python built-in operation
    combined with an operation from our custom Complex class.
    Both are complex numbers.
    """
    assert ((2 + 3j) - (Complex(1, 2))) == Complex(1, 1)
Exemple #19
0
def roots(a, b, c):
    check = (b**2) - (4 * a * c)
    if check > 0:  # 2 real roots
        root1 = ((-b) + math.sqrt(check)) / (a * 2)
        root2 = ((-b) - math.sqrt(check)) / (a * 2)
        tup = (root1, root2)
        return tup

    elif check < 0:  # 2 complex root
        root1 = Complex()
        root2 = Complex()

        root1.im = math.sqrt(abs(check)) / (a * 2)
        root2.im = -(math.sqrt(abs(check)) / (a * 2))

        root1.re = (-b) / (a * 2)
        root2.re = (-b) / (a * 2)

        tup = (root1, root2)
        return tup

    else:  # 1 real root
        root1 = (-b) / (a * 2)
        tup = (root1, )
        return (tup)
Exemple #20
0
def test_rmul():
    """Test to make sure the __rmul__ function works."""
    complexNumber = Complex(2, 1)
    assert (complexNumber * 2) == Complex(4, 2), error

    complexNumber = Complex(-1, 4)
    assert (complexNumber * 4) == Complex(-4, 16), error
Exemple #21
0
def test_conjugate():
    """Raises AssertionError if conjugating Complex objects does not return
    expected values.
    """
    z = Complex(-5, 3)
    ans = z.conjugate()
    expected = Complex(-5, -3)
    assert ans.real == expected.real and ans.imag == expected.imag
Exemple #22
0
def RY(phi):
    return np.array(
        [[Complex(math.cos(phi / 2), 0),
          Complex(0,
                  math.sin(phi / 2) * 1)],
         [Complex(0,
                  math.sin(phi / 2) * (-1)),
          Complex(math.cos(phi / 2), 0)]])
Exemple #23
0
def test_subtraction():
    """Raises AssertionError if subtrcting Complex objects does not return
    expected values.
    """
    z = Complex(-5, 3)
    w = Complex(11, -7)
    ans = z - w
    expected = Complex(-16, 10)
    assert ans.real == expected.real and ans.imag == expected.imag
 def build_equal_opportunity_qubit():
     """
     This method creates a qubit with equal-opportunity
     :return: equal-opportunity qubit 
     """
     equal_opportunity_number = 1 / pow(2, 0.5)
     alpha_function = Complex(equal_opportunity_number, 0)
     beta_function = Complex(equal_opportunity_number, 0)
     return Qubit(alpha_function, beta_function)
Exemple #25
0
def test_init_r(fix1, r, theta, expected_a, expected_b):
    print("\n\n\nTesting init...\n")
    if r < 0:
        with pytest.raises(ValueError):
            Complex(r=r, theta=theta)
    else:
        znumber = Complex(r=r, theta=theta)
        assert round(znumber.a, 6) == expected_a
        assert round(znumber.b, 6) == expected_b
Exemple #26
0
def test_addition():
    """Raises AssertionError if adding Complex objects does not return
    expected values.
    """
    z = Complex(-5, 3)
    w = Complex(11, -7)
    ans = z + w
    expected = Complex(6, -4)
    assert ans.real == expected.real and ans.imag == expected.imag
Exemple #27
0
def string_return():
    try:
        print('### String Return ###')
        a = Complex(1, 2)
        b = Complex(1, -2)
        print(a)
        print(b)
    except:
        print("Complex number return failed")
Exemple #28
0
def construct():
    try:
        a = Complex(1.0, 2.3)  # 1 + 2.3i
        b = Complex(2)  # 2 + 0i
        c = Complex()  # 0 + 0i
        print('Class construction passed')

    except:
        print('Class construction failed')
Exemple #29
0
    def test_adding_negative_to_positive_reults_negative(self):
        x = Complex(1, 2)
        y = Complex(-2, -3)
        result = x + y

        self.assertEqual(
            str(result), "-1.00 - 1.00i",
            "Adding a negative complex to a positive complex number didn't print the correct value"
        )
Exemple #30
0
def roots(a, b, c):
    n = b**2 - 4 * a * c
    if n > 0:
        return (-b + math.sqrt(n)) / (2 * a), (-b - math.sqrt(n)) / (2 * a)
    elif n == 0:
        return -b / (2 * a)
    else:
        real = -b / (2 * a)
        imag = math.sqrt(-n) / (2 * a)
        return Complex(real, imag), Complex(real, -imag)