> ## Documentation Index
> Fetch the complete documentation index at: https://docs.haiqu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Postprocess Measurement Counts

> Improve sampled QUBO solutions with Haiqu's classical post-processing.

Use this tool after a QAOA or VQE sampling run to turn noisy measured
bitstrings into better solutions. It applies bit-flip local search against the
QUBO objective and runs synchronously, so results come back immediately.

When to Use:
    - Call this after ``run_circuits_on_qpu_or_simulator`` returns counts for a
      QUBO problem, which is the last step of the QAOA workflow.
    - Call this when the user asks for the best solution or objective value
      rather than the raw measurement distribution.

Constraints:
    - Provide exactly one of ``counts`` or ``run_job_id``. Prefer
      ``run_job_id`` so large count dictionaries stay out of the tool call.
    - ``lp_problem`` must be the same objective the circuit was built from,
      otherwise the reported costs are meaningless.
    - Only the best ``max_solutions`` solutions are returned; the response
      reports ``total_solutions`` and sets ``truncated`` when it omits some.

Notes:
    - Report ``best_bitstring`` and ``best_cost`` first; the solution list is
      supporting detail.
    - Implausible costs usually mean the LP problem does not match the circuit
      that produced the counts, not a post-processing failure.

Args:
    user: Authenticated user resolved from the API key.
    data: Problem definition plus inline counts or a run job reference.
    db: Active database session.

Returns:
    The best solutions found, ordered by objective value, with a summary.

Raises:
    HTTPException: Raised with ``400`` when neither or both count inputs are
        given, ``404`` when a referenced job is unavailable, ``409`` when the
        referenced job has no usable counts, or ``500`` when post-processing
        fails.



## OpenAPI

````yaml https://api.haiqu.ai/openapi.json post /ai/postprocess_measurement_counts
openapi: 3.1.0
info:
  title: Haiqu API
  summary: Haiqu RESTful API service.
  description: Cloud service providing the access to Haiqu cloud runtime
  contact:
    name: Haiqu Inc.
    url: https://haiqu.ai/
    email: info@haiqu.ai
  version: 1.5.0
servers: []
security: []
paths:
  /ai/postprocess_measurement_counts:
    post:
      summary: Postprocess Measurement Counts
      description: >-
        Improve sampled QUBO solutions with Haiqu's classical post-processing.


        Use this tool after a QAOA or VQE sampling run to turn noisy measured

        bitstrings into better solutions. It applies bit-flip local search
        against the

        QUBO objective and runs synchronously, so results come back immediately.


        When to Use:
            - Call this after ``run_circuits_on_qpu_or_simulator`` returns counts for a
              QUBO problem, which is the last step of the QAOA workflow.
            - Call this when the user asks for the best solution or objective value
              rather than the raw measurement distribution.

        Constraints:
            - Provide exactly one of ``counts`` or ``run_job_id``. Prefer
              ``run_job_id`` so large count dictionaries stay out of the tool call.
            - ``lp_problem`` must be the same objective the circuit was built from,
              otherwise the reported costs are meaningless.
            - Only the best ``max_solutions`` solutions are returned; the response
              reports ``total_solutions`` and sets ``truncated`` when it omits some.

        Notes:
            - Report ``best_bitstring`` and ``best_cost`` first; the solution list is
              supporting detail.
            - Implausible costs usually mean the LP problem does not match the circuit
              that produced the counts, not a post-processing failure.

        Args:
            user: Authenticated user resolved from the API key.
            data: Problem definition plus inline counts or a run job reference.
            db: Active database session.

        Returns:
            The best solutions found, ordered by objective value, with a summary.

        Raises:
            HTTPException: Raised with ``400`` when neither or both count inputs are
                given, ``404`` when a referenced job is unavailable, ``409`` when the
                referenced job has no usable counts, or ``500`` when post-processing
                fails.
      operationId: postprocess_measurement_counts
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ContextPostprocessSubmitModel'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ContextPostprocessResultModel'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyQuery: []
        - APIKeyHeader: []
components:
  schemas:
    ContextPostprocessSubmitModel:
      properties:
        lp_problem:
          type: string
          title: Lp Problem
          description: QUBO problem serialized in LP file format.
        counts:
          anyOf:
            - additionalProperties:
                type: number
              type: object
            - type: 'null'
          title: Counts
          description: >-
            Measured bitstring counts, for example `{'0101': 120, '1010': 87}`.
            Provide this or `run_job_id`, not both.
        run_job_id:
          anyOf:
            - type: string
            - type: 'null'
          title: Run Job Id
          description: >-
            Run job whose stored measurement results are used as `counts`, so
            large count dictionaries never have to be passed inline. Provide
            this or `counts`.
        params:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Params
          description: >-
            Optional overrides: `method` (`none`, `bitflip_incremental`,
            `bitflip_steepest_descent`), `postprocess_iterations`,
            `use_fast_eval`, `seed`.
        max_solutions:
          type: integer
          maximum: 500
          minimum: 1
          title: Max Solutions
          description: Maximum number of solutions returned inline, best cost first.
          default: 20
      type: object
      required:
        - lp_problem
      title: ContextPostprocessSubmitModel
      description: |-
        Define payload for post-processing QUBO measurement counts.

        Attributes:
            lp_problem: QUBO problem in LP file format.
            counts: Measured bitstring counts to optimize.
            run_job_id: Run job whose stored results supply ``counts``.
            params: Optional post-processing parameter overrides.
            max_solutions: Cap on how many solutions are returned inline.
      examples:
        - lp_problem: |-
            Minimize
             obj: x0 + x1
            Binary
             x0
             x1
            End
          params:
            postprocess_iterations: 10
            seed: 42
          run_job_id: jb-abc123
        - counts:
            '00': 100
            '01': 50
          lp_problem: |-
            Minimize
             obj: x0 + x1
            Binary
             x0
             x1
            End
          max_solutions: 5
    ContextPostprocessResultModel:
      properties:
        best_bitstring:
          anyOf:
            - type: string
            - type: 'null'
          title: Best Bitstring
        best_cost:
          anyOf:
            - type: number
            - type: 'null'
          title: Best Cost
        solutions:
          items:
            additionalProperties: true
            type: object
          type: array
          title: Solutions
        returned_solutions:
          type: integer
          title: Returned Solutions
        total_solutions:
          type: integer
          title: Total Solutions
        truncated:
          type: boolean
          title: Truncated
        context:
          type: string
          title: Context
      type: object
      required:
        - solutions
        - returned_solutions
        - total_solutions
        - truncated
        - context
      title: ContextPostprocessResultModel
      description: |-
        Represent post-processed QUBO solutions, truncated for tool output.

        Attributes:
            best_bitstring: Lowest-cost solution found.
            best_cost: Objective value of ``best_bitstring``.
            solutions: Best solutions as ``bitstring`` / ``cost`` / ``count``
                entries, ordered by cost.
            returned_solutions: Number of entries in ``solutions``.
            total_solutions: Number of solutions the post-processor produced.
            truncated: Whether ``solutions`` omits some results.
            context: Summary of the outcome and how to read it.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyQuery:
      type: apiKey
      in: query
      name: HAIQU_API_KEY
    APIKeyHeader:
      type: apiKey
      in: header
      name: authorization

````