def test_get_depth(self): """ Tests that the get_depth method returns the correct depth, as defined by IBM. """ circuit = ic.IBMCircuit(10) w1 = iw.IBMInputWire("w1", circuit) g1 = igac.IBMAddConstGate("g1", w1, ib.IBMBatch([True, True]), circuit) g2 = igm.IBMMulGate("g2", g1, w1, circuit) g3 = igac.IBMAddConstGate("g3", g2, ib.IBMBatch([True, True]), circuit) self.assertEqual(0, g1.get_depth()) self.assertEqual(1, g2.get_depth()) self.assertEqual(1, g3.get_depth())
def make_random_one_inp_and_const_inp_gate(L, ultimate_level, penultimate_level, gate_name, circuit, gate_factory): """creates a random gate with one input and a constant that is an input batch.""" # This gate requires one input; at least one input should be available. assert(len(ultimate_level) + len(penultimate_level) > 0) input1_index = sr.randint(0, len(ultimate_level) - 1) input1 = ultimate_level[input1_index] const = ci.Input([ib.IBMBatch([int(sr.getrandbits(1)) for inp_ind in xrange(L)])]) return gate_factory(gate_name, input1, const, circuit)
def test_get_depth(self): """ Tests that the get_depth method returns the correct depth, as defined by IBM. """ circuit = ic.IBMCircuit(10) w1 = iw.IBMInputWire("w1", circuit) w2 = iw.IBMInputWire("w2", circuit) const = ci.Input([ib.IBMBatch([1, 0, 1])]) g1 = igs.IBMSelectGate("g1", w1, w2, const, circuit) self.assertEqual(.6, g1.get_depth())
def test_get_full_display_string(self): """ Tests that the method get_full_display_string returns the correct string. """ #Initialize the circuit: circuit = ic.IBMCircuit(10) #Initialize the input wire: w1 = iw.IBMInputWire("w1", circuit) #Initialize the constnat: const = ci.Input([ib.IBMBatch([True, False])]) #Initialize the gate: g = igmc.IBMMulConstGate("g", w1, const, circuit) self.assertEquals("g:LMULconst(w1,[10])", g.get_full_display_string())
def test_get_full_display_string(self): """ Tests that the method get_full_display_string returns the correct string. """ #Initialize the circuit: circuit = ic.IBMCircuit(10) #Initialize the input wires: w1 = iw.IBMInputWire("w1", circuit) w2 = iw.IBMInputWire("w2", circuit) #Initialize the constant: const = ci.Input([ib.IBMBatch([1, 0])]) #Initialize the gate: g = igs.IBMSelectGate("g", w1, w2, const, circuit) self.assertEquals(g.get_full_display_string(), "g:LSELECT(w1,w2,[10])")
def make_random_two_inp_and_const_inp_gate(L, ultimate_level, penultimate_level, gate_name, circuit, gate_factory): """creats a random gate with two inputs and a constant that is an input batch.""" # This gate requires two inputs; at least two inputs should be available. assert(len(ultimate_level) + len(penultimate_level) > 1) input1_index = sr.randint(0, len(ultimate_level) - 1) input1 = ultimate_level[input1_index] input2_index = sr.randint(0, len(ultimate_level) + len(penultimate_level) - 1) while input2_index == input1_index: input2_index = sr.randint(0, len(ultimate_level) + len(penultimate_level) - 1) if input2_index < len(ultimate_level): input2 = ultimate_level[input2_index] else: input2 = penultimate_level[input2_index - len(ultimate_level)] const = ci.Input([ib.IBMBatch([int(sr.getrandbits(1)) for inp_ind in xrange(L)])]) return gate_factory(gate_name, input1, input2, const, circuit)
def make_random_input(L, W, B): """Creates a random input with W batches, each with L bits.""" return ci.Input([ ib.IBMBatch([int(sr.randint(0, B)) for inp_num in xrange(L)]) for batch_num in xrange(W) ])
def test_get_num_values(self): batch = ib.IBMBatch([1, 0, 1, 0, 0]) self.assertEqual(2, batch.get_num_values(1)) self.assertEqual(3, batch.get_num_values(0))
def test_display(self): """ Tests the display method in the IBMBatch class works as intended. """ batch = ib.IBMBatch([1,2,3]) self.assertEqual("123", str(batch))