def test_qmor(self): """ Tests whether qmor is the same as 2 qor gates """ a = qbool(self.qlcass, prob=0.7) b = qbool(self.qclass, prob=0.4) c = qbool(self.qclass, prob=0.9) control = [b, c] result = a.qmor(control) self.assertListEqual(control, [b, c]) other = a.qmor(b).qmor(c) result.measure() other.measure() counts = self.qclass.get_counts() resultCounts = result.extract_counts(counts) otherCounts = other.extract_counts() toTest = [] for k, v in resultCounts.items(): try: otherVal = otherCounts[k] toTest.append((v, otherVal)) except KeyError: toTest.append((v, 0)) self.assertTrue(kinda_close_tuples(toTest))
def test_free(self): """ Tests whether free returns the qubit """ a = qbool(self.qclass, prob=0.7) b = qbool(self.qclass, prob=0.35) c = a.qand(b) c.iqand(a, b) before = self.qclass.bitsLeft c.free() self.assertEqual(self.qclass.bitsLeft, before + 1)
def test_qif(self): """ Tests whether qif works properly """ a = qbool(self.qclass, prob=0.5) b = qbool(self.qclass, prob=0.5) c = a.qif(b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertTrue([(counts[True], 0.75), (counts[False], 0.25)])
def test_qnand(self): """ Tests the qnand on 2 independent qbools """ a = qbool(self.qclass, prob=0.5) b = qbool(self.qclass, prob=0.5) c = a.qnand(b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertTrue([(counts[True], 0.75), (counts[False], 0.25)])
def test_qor(self): """ Tests whether qor properly ors independent qbools """ a = qbool(self.qclass, prob=0.5) b = qbool(self.qclass, prob=0.5) c = a.qor(b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertTrue([(counts[True], 0.75), (counts[False], 0.25)])
def test_iqif(self): """ Tests whether iqif actually inverts qif """ a = qbool(self.qclass, prob=0.7) b = qbool(self.qclass, prob=0.35) c = a.qif(b) c.iqif(a, b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertRaises( KeyError, counts[True]) #Tests that True is not in the possibilities self.assertTrue(False in counts.keys())
def tests_qxor_vs_qiff(self): """ Tests whether qxor is equivalent to not qiff """ a = qbool(self.qclass, prob=0.5) b = qbool(self.qclass, prob=0.5) c = a.qxor(b) d = a.qiff(b) c.measure() d.measure() counts = self.qclass.get_counts() ccounts = c.extract_counts(counts) dcounts = d.extract_counts(counts) self.assertEqual(ccounts[True], dcounts[False]) self.assertEqual(ccounts[False], dcounts[True])
def test_qnand_vs_qand(self): """ Tests whether qand and qnand are opposites """ a = qbool(self.qclass, prob=0.5) b = qbool(self.qclass, prob=0.5) c = a.qnand(b) d = a.qand(b) c.measure() d.measure() counts = self.qclass.get_counts() ccounts = c.extract_counts(counts) dcounts = d.extract_counts(counts) self.assertEqual(ccounts[True], dcounts[False]) self.assertEqual(ccounts[False], dcounts[True])
def test_qor_entangled(self): a = qbool(self.qclass, prob=0.5) b = a.entangle() c = a.qor(b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertTrue([(counts[False], 0.5), (counts[True], 0.5)])
def test_init(self): """ Tests whether initial value is properly assigned """ base = qbool(self.qclass, initial=True) base.measure() result = self.qclass.get_result() self.assertTrue(base.extract_result())
def test_base_is_false(self): """ Tests whether qbool properly initializes to false with no initial value """ base = qbool(self.qclass) base.measure() result = self.qclass.get_result() self.assertFalse(base.extract_result())
def test_qand_entangled(self): """ Tests whether qand properly ands entangled qbools """ a = qbool(self.qclass, prob=0.5) b = a.entangle() c = a.qand(b) c.measure() counts = self.qclass.get_counts() counts = c.extract_counts(counts) self.assertTrue([(counts[False], 0.5), (counts[True], 0.5)])
def test_prob(self): """ Tests whether a qbool initialized with a certain probability actually measures close to said prob """ base = qbool(self.qclass, prob=0.75) base.measure() counts = self.qclass.get_counts() counts = base.extract_counts(counts) self.assertTrue( kinda_close_tuples([(counts[True] / 1024, 0.75), (counts[False] / 1024, 0.25)]))
def test_prob_not(self): """ Tests whether qnot properly nots a superposition """ base = qbool(self.qclass, prob=0.75) base.qnot() base.measure() counts = self.qclass.get_counts() counts = base.extract_counts(counts) self.assertTrue( kinda_close_tuples([(counts[True] / 1024, 0.25), (counts[False] / 1024, 0.75)]))
def test_entangle(self): """ Tests whether an entangled qbool works """ base = qbool(self.qclass, prob=0.5) entangled = base.entangle() base.measure() entangled.measure() counts = self.qclass.get_counts() baseCounts = base.extract_counts(counts) entangledCounts = entangled.extract_counts(counts) self.assertEqual(baseCounts[False], entangledCounts[False]) self.assertEqual(baseCounts[True], entangledCounts[True])