public void TestQubitManager() { QubitManager qm = new QubitManager(30); // Test allocation of single qubit Qubit q1 = qm.Allocate(); Assert.True(q1.Id == 0); // Test allocation of multiple qubits IQArray <Qubit> qa1 = qm.Allocate(4); Assert.True(qa1.Length == 4); Assert.True(qa1[0].Id == 1); Assert.True(qa1[1].Id == 2); Assert.True(qa1[2].Id == 3); Assert.True(qa1[3].Id == 4); // Test reuse of deallocated qubits qm.Release(qa1[1]); Qubit q2 = qm.Allocate(); Assert.True(q2.Id == 2); IQArray <Qubit> qa2 = qm.Allocate(3); Assert.True(qa2.Length == 3); Assert.True(qa2[0].Id == 5); Assert.True(qa2[1].Id == 6); Assert.True(qa2[2].Id == 7); qm.Release(qa2); Qubit q3 = qm.Allocate(); Assert.True(q3.Id == 7); Qubit q4 = qm.Allocate(); Assert.True(q4.Id == 6); Qubit q5 = qm.Allocate(); Assert.True(q5.Id == 5); // Test borrowing HashSet <Qubit> exclusion = new HashSet <Qubit>(); exclusion.Add(qa1[0]); exclusion.Add(qa1[2]); exclusion.Add(q4); exclusion.Add(q3); long qubitsAvailable; qubitsAvailable = qm.FreeQubitsCount; IQArray <Qubit> qab = qm.Borrow(5, exclusion); Assert.True(qubitsAvailable - qm.FreeQubitsCount == 1); Assert.True(qab[0].Id == 0); Assert.True(qab[1].Id == 2); Assert.True(qab[2].Id == 4); Assert.True(qab[3].Id == 5); Assert.True(qab[4].Id == 8); Qubit q6 = qm.Allocate(); Assert.True(q6.Id == 9); // Test borrowing of the same qubit again qubitsAvailable = qm.FreeQubitsCount; IQArray <Qubit> qb1 = qm.Borrow(1, exclusion); Assert.True(qubitsAvailable - qm.FreeQubitsCount == 0); Assert.True(qb1[0].Id == 0); qm.Return(qb1[0]); qm.Return(qab); // Test that qubit allocated for borrowing is freed after being returned Qubit q7 = qm.Allocate(); Assert.True(q7.Id == 8); // Test allocation of qubits out of order. qm.Release(q4); qm.Release(qa1[2]); qm.Release(q1); IQArray <Qubit> qa3 = qm.Allocate(4); Assert.True(qa3.Length == 4); Assert.True(qa3[0].Id == 0); Assert.True(qa3[1].Id == 3); Assert.True(qa3[2].Id == 6); Assert.True(qa3[3].Id == 10); // Test Disabling qubits Qubit q8 = qm.Allocate(); Assert.True(q8.Id == 11); qm.Disable(q8); IQArray <Qubit> qab2 = qm.Borrow(12); Assert.True(qab2[11].Id == 23); // make sure 11 is not borrowed. qm.Release(q8); qm.Return(qab2); IQArray <Qubit> qa4 = qm.Allocate(2); Assert.True(qa4[0].Id == 23); // make sure 11 is not reused. Assert.True(qa4[1].Id == 22); // make sure 11 is not reused. qm.Release(qa4); { // Test allocating zero qubits IQArray <Qubit> n_q; n_q = qm.Allocate(0); Assert.True(n_q.Length == 0); n_q = qm.Borrow(0, exclusion); Assert.True(n_q.Length == 0); } // NOTE: The below tests trigger exceptions, which but the QubitManager into a bad // state where it shouldn't be reused. Creating a separate QubitManager in a small // scope to test the exceptions avoids having one test case pollute the other. // Test for over allocating and over borrowing. { QubitManager qm_small = new QubitManager(2); IQArray <Qubit> n_q; Assert.Throws <NotEnoughQubits>(() => n_q = qm_small.Allocate(5)); } { QubitManager qm_small = new QubitManager(2); IQArray <Qubit> n_q; Assert.Throws <NotEnoughQubits>(() => n_q = qm_small.Borrow(5)); } // Test for negative input to allocate and borrow. { QubitManager qm_small = new QubitManager(20); IQArray <Qubit> n_q; Assert.Throws <ArgumentException>(() => n_q = qm_small.Allocate(-2)); } { QubitManager qm_small = new QubitManager(20); IQArray <Qubit> n_q; Assert.Throws <ArgumentException>(() => n_q = qm_small.Borrow(-2)); } }
public void TestQubitManager() { QubitManager qm = new QubitManager(20); // Test allocation of single qubit Qubit q1 = qm.Allocate(); Assert.True(q1.Id == 0); // Test allocation of multiple qubits var qa1 = qm.Allocate(4); Assert.True(qa1.Length == 4); Assert.True(qa1[0].Id == 1); Assert.True(qa1[1].Id == 2); Assert.True(qa1[2].Id == 3); Assert.True(qa1[3].Id == 4); // Test reuse of deallocated qubits qm.Release(qa1[1]); Qubit q2 = qm.Allocate(); Assert.True(q2.Id == 2); var qa2 = qm.Allocate(3); Assert.True(qa2.Length == 3); Assert.True(qa2[0].Id == 5); Assert.True(qa2[1].Id == 6); Assert.True(qa2[2].Id == 7); qm.Release(qa2); Qubit q3 = qm.Allocate(); Assert.True(q3.Id == 7); Qubit q4 = qm.Allocate(); Assert.True(q4.Id == 6); Qubit q5 = qm.Allocate(); Assert.True(q5.Id == 5); // Test borrowing Qubit[] exclusion = new Qubit[4]; exclusion[0] = qa1[0]; exclusion[1] = qa1[2]; exclusion[2] = q4; exclusion[3] = q3; long qubitsAvailable; qubitsAvailable = qm.GetFreeQubitsCount(); var qab = qm.Borrow(5, exclusion); Assert.True(qubitsAvailable - qm.GetFreeQubitsCount() == 1); Assert.True(qab[0].Id == 0); Assert.True(qab[1].Id == 2); Assert.True(qab[2].Id == 4); Assert.True(qab[3].Id == 5); Assert.True(qab[4].Id == 8); Qubit q6 = qm.Allocate(); Assert.True(q6.Id == 9); // Test borrowing of the same qubit again qubitsAvailable = qm.GetFreeQubitsCount(); var qb1 = qm.Borrow(1, exclusion); Assert.True(qubitsAvailable - qm.GetFreeQubitsCount() == 0); Assert.True(qb1[0].Id == 0); qm.Return(qb1[0]); qm.Return(qab); // Test that qubit allocated for borrowing is freed after being returned Qubit q7 = qm.Allocate(); Assert.True(q7.Id == 8); // Test allocation of qubits out of order. qm.Release(q4); qm.Release(qa1[2]); qm.Release(q1); var qa3 = qm.Allocate(4); Assert.True(qa3.Length == 4); Assert.True(qa3[0].Id == 0); Assert.True(qa3[1].Id == 3); Assert.True(qa3[2].Id == 6); Assert.True(qa3[3].Id == 10); // Test Disabling qubits Qubit q8 = qm.Allocate(); Assert.True(q8.Id == 11); qm.Disable(q8); var qab2 = qm.Borrow(12, null); Assert.True(qab2[11].Id == 12); // make sure 11 is not borrowed. qm.Release(q8); qm.Return(qab2); var qa4 = qm.Allocate(2); Assert.True(qa4[0].Id == 12); // make sure 11 is not reused. Assert.True(qa4[1].Id == 13); // make sure 11 is not reused. qm.Release(qa4); // Test allocating qubits over capacity OperationsTestHelper.IgnoreDebugAssert(() => { IQArray <Qubit> n_q; Assert.Throws <NotEnoughQubits>(() => n_q = qm.Allocate(10)); Assert.Throws <NotEnoughQubits>(() => n_q = qm.Borrow(25, exclusion)); Assert.Throws <ArgumentException>(() => n_q = qm.Allocate(0)); Assert.Throws <ArgumentException>(() => n_q = qm.Allocate(-2)); Assert.Throws <ArgumentException>(() => n_q = qm.Borrow(0, exclusion)); Assert.Throws <ArgumentException>(() => n_q = qm.Borrow(-2, exclusion)); }); }