def test_quadratic_form_parameterized(self): """Test the quadratic form circuit with parameters.""" theta = ParameterVector("th", 7) p_quadratic = [[theta[0], theta[1]], [theta[2], theta[3]]] p_linear = [theta[4], theta[5]] p_offset = theta[6] quadratic = np.array([[2, 1], [-1, -2]]) linear = np.array([2, 0]) offset = 0 m = 2 circuit = QuadraticForm(m, p_quadratic, p_linear, p_offset) param_dict = dict(zip(theta, [*quadratic[0]] + [*quadratic[1]] + [*linear] + [offset])) circuit.assign_parameters(param_dict, inplace=True) self.assertQuadraticFormIsCorrect(m, quadratic, linear, offset, circuit)
def test_required_result_qubits(self): """Test getting the number of required result qubits.""" with self.subTest("positive bound"): quadratic = [[1, -50], [100, 0]] linear = [-5, 5] offset = 0 num_result_qubits = QuadraticForm.required_result_qubits(quadratic, linear, offset) self.assertEqual(num_result_qubits, 1 + int(np.ceil(np.log2(106 + 1)))) with self.subTest("negative bound"): quadratic = [[1, -50], [10, 0]] linear = [-5, 5] offset = 0 num_result_qubits = QuadraticForm.required_result_qubits(quadratic, linear, offset) self.assertEqual(num_result_qubits, 1 + int(np.ceil(np.log2(55)))) with self.subTest("empty"): num_result_qubits = QuadraticForm.required_result_qubits([[]], [], 0) self.assertEqual(num_result_qubits, 1)
def _get_a_operator(self, qr_key_value, problem): quadratic = problem.objective.quadratic.to_array() linear = problem.objective.linear.to_array() offset = problem.objective.constant # Get circuit requirements from input. quadratic_form = QuadraticForm(self._num_value_qubits, quadratic, linear, offset, little_endian=False) a_operator = QuantumCircuit(qr_key_value) a_operator.h(list(range(self._num_key_qubits))) a_operator.compose(quadratic_form, inplace=True) return a_operator
def test_quadratic_form(self): """Test the quadratic form circuit.""" with self.subTest("empty"): circuit = QuadraticForm() self.assertQuadraticFormIsCorrect(1, [[0]], [0], 0, circuit) with self.subTest("1d case"): quadratic = np.array([[1]]) linear = np.array([2]) offset = -1 circuit = QuadraticForm(quadratic=quadratic, linear=linear, offset=offset) self.assertQuadraticFormIsCorrect(3, quadratic, linear, offset, circuit) with self.subTest("negative"): quadratic = np.array([[-2]]) linear = np.array([0]) offset = -1 m = 2 circuit = QuadraticForm(m, quadratic, linear, offset) self.assertQuadraticFormIsCorrect(m, quadratic, linear, offset, circuit) with self.subTest("missing quadratic"): quadratic = np.zeros((3, 3)) linear = np.array([-2, 0, 1]) offset = -1 circuit = QuadraticForm(linear=linear, offset=offset) self.assertQuadraticFormIsCorrect(3, quadratic, linear, offset, circuit) with self.subTest("missing linear"): quadratic = np.array([[1, 2, 3], [3, 1, 2], [2, 3, 1]]) linear = np.zeros(3) offset = -1 m = 2 circuit = QuadraticForm(m, quadratic, None, offset) self.assertQuadraticFormIsCorrect(m, quadratic, linear, offset, circuit) with self.subTest("missing offset"): quadratic = np.array([[2, 1], [-1, -2]]) linear = np.array([2, 0]) offset = 0 m = 2 circuit = QuadraticForm(m, quadratic, linear) self.assertQuadraticFormIsCorrect(m, quadratic, linear, offset, circuit)
def test_endian(self, little_endian): """Test the outcome for different endianness.""" qform = QuadraticForm(2, linear=[0, 1], little_endian=little_endian) circuit = QuantumCircuit(4) circuit.x(1) circuit.compose(qform, inplace=True) # the result is x_0 linear_0 + x_1 linear_1 = 1 = '0b01' result = "01" # the state is encoded as |q(x)>|x>, |x> = |x_1 x_0> = |10> index = (result if little_endian else result[::-1]) + "10" ref = np.zeros(2 ** 4, dtype=complex) ref[int(index, 2)] = 1 self.assertTrue(Statevector.from_instruction(circuit).equiv(ref))