ubsuny / 23-Homework1G1

Homework 1 Repository Group 1
0 stars 6 forks source link

Code does not work with mixed numbers #39

Closed AhmedCode99 closed 12 months ago

AhmedCode99 commented 12 months ago

The code does not work when multiplying a positive and a negative number. Any ideas on how to fix it?

WildJimmy commented 12 months ago

Hi Ahmed,

I have no idea how one would encode the fact that the number was negative into qubits from binary (and frankly it scares me), but you could probably get away with making some conditional statement at the beginning that slaps a negative sign on. I.e.

sign = 1
  if (multiplicand < 0) != (multiplier < 0):
    sign = -1
  multiplier = abs(multiplier)
  multiplicand = abs(multiplicand)

Where since the rest of the code is unfriendly to binary representations of negative numbers (because I think that gets weird), once we've tracked the sign we repeat the normal code with the absolute value of the two numbers. We just make sure to return at the end return sign*int(list(result.get_counts())[0],2)

Which on my end returns the correct value for negative numbers multiplied by positive and vice versa. I think this should be acceptable since like I said I don't know how one would encode a negative number into qubits, though that would be interesting to look into...