# Dirac-3 Quick Start

#### Device: Dirac-3

Entropy quantum computing (EQC) is a unique tool for optimization leveraging exotic effects in open quantum systems, including the quantum Zeno effect. Most quantum and quantum-inspired programming models, including quantum annealing models, are restricted to qubits. Dirac-3 hybrid quantum optimization machine, 3rd generation of EQC, allows the use of qudits, which are units of quantum information similar to qubits, but each taking more than two possible values. Be sure to read the Qudit Basics to further understand the benefit of high dimensional encoding using qudits.

This tutorial serves as a quick starter guide, offering practical examples to help users learn the solver characteristics, submit problem, and interpret the results from Dirac-3 effectively.

## Device - Dirac-3

Dirac-3 solves quadratic Hamiltonians of up to 949 variables. It also solves highly complex problems with three and four-body interaction. For more details, see the user guide.

## Tutorial Structure

The goal of this tutorial is to explain how to work with Dirac-3.

The first step is to turn an objective function into a Hamiltonian. It is important that the Hamiltonian is important in Hermitian form. That means the interaction matrices or tensors must be symmetric upon conjugation or transposition operations.

For example, if the objective function is

Then the Hamiltonian in the matrix form shall be (assuming $b$ is real)

for state vector $\begin{bmatrix}x_1 \\ x_2 \end{bmatrix}$. Note that cross-term in the second part of the Hamiltonian is $b/2$. To code the Hamiltonian into Dirac-3, the linear and quadratic parts are defined separately as

and

These two matrices are input into the `sample_hamiltonian`

method in the examples below.

In the following, we will use three different examples to illustrate how to prepare and submit problems.

- The first is a simple example to help with understanding the use of the qudits.
- The second example is a polynomial which will be used for demonstrating how different values of the summation constraint can be used to change the solution of the problem.
- The third example is a simple problem which, like the first, is minimized by setting one value to the whole quantity of $R$. The difference is that there are three solutions at optimality. Repeated runs will reveal different solutions.

First, we'll get our Python environment set up. We need `numpy`

, two methods to sample and evaluate quadratic Hamiltonians, respectively, and the connection to a device.

In [1]:

`import numpy as npimport matplotlib.pyplot as pltfrom qci_client import QciClientimport osurl = "https://api.qci-prod.com"q1 = QciClient(api_token=os.getenv("QCI_TOKEN"), url=url)`

The `sample_hamiltonian`

method takes six required arguments. `C`

and `J`

are the linear and quadratic terms of the polynomial. These matrices are dense and the method converts into a sparse format for the `process_job`

call. The `sum_constraint`

parameter is a value that indicates the total returned given solution vector must add up to. The `schedule`

parameter takes four different values, 1, 2, 3 or 4.

- This is the quickest execution. It has the lowest probability of obtaining high-quality solutions.
- This execution takes more time, likely a few seconds, but has a higher probability of finding good solutions.
- This option may take tens of seconds. The quality of these solutions is expected to be higher.
- This is the longest-running relaxation option. It takes up to multiple minutes to run and has the highest probability of returning quality solutions.

The `solution_precision`

parameter is a value that indicates how granular a solution should be. Use a value of 1 for integer solutions. Use decimals such as 0.1 or 0.05 for higher precision with the highest precision accepted being 0.00001. Specify a connected `EqcClient`

in the `client`

parameter. Suppress print statements in the call using a value of `True`

for `suppress_output`

.

In [2]:

`def sample_hamiltonian(C : np.ndarray, J : np.ndarray, sum_constraint : float, schedule : int, solution_precision : float, client : QciClient):n = C.shape[0]H = np.hstack([C.reshape([n, 1]), J])ham_file = {"file_name": "qudit-tutorial-hame", "file_config": {"hamiltonian": {"data": H}}}file_id = client.upload_file(file=ham_file)["file_id"]job_tags = ["qudit-tutorial"]job_body = client.build_job_body(job_type="sample-hamiltonian", hamiltonian_file_id=file_id, job_params={"device_type": "dirac-3", "num_samples": 1, "solution_precision": solution_precision,"sum_constraint": sum_constraint, "relaxation_schedule": schedule}, job_tags=job_tags)response = client.process_job(job_body=job_body, wait=True)return responsedef get_results(response):if "results" in response and response["results"] is not None:results = response["results"]else:if "job_info" in response and "job_result" in response["job_info"]:details = response["job_info"]["job_result"]else:details = Noneraise RuntimeError(f"Execution failed. See details: {details}")return results`

## Qudit Domain

Dirac-3 uses qudits to solve discrete optimization problems.

The first example is a three-variable problem with uniform two-body interaction, whose solution corresponds to one variable taking on the total value of the summation constraint and the rest taking the value 0.

In the coding please note the `dtype`

of `float32`

. This is the digital precision for floating point numbers used in the gRPC protocol. A warning will be raised if higher precision decimal values are used.

In [3]:

`h = np.array([[-1],[0],[0]], dtype=np.float32)J= np.array([[0, 1, 1],[1, 0, 1],[1, 1, 0]], dtype=np.float32)h, J`

Out [3]:

(array([[-1.], [ 0.], [ 0.]], dtype=float32), array([[0., 1., 1.], [1., 0., 1.], [1., 1., 0.]], dtype=float32))

In [5]:

`response = sample_hamiltonian(h, J, 400, 1, 0.1, q1)if "job_info" in response:print("Status:", response["job_info"]["job_status"])results = get_results(response)print("Energy:", results["energies"][0])print("Solution")print(results["solutions"][0])solution = np.array(results["solutions"][0])print("Solution Value (should match energy)", h.T@solution + solution.T@J@solution)x = np.array([400, 0, 0])print("Known ground state", h.T@x + x.T@J@x)print("With solution", x)else:print(response)`

Out [ ]:

2024-05-08 09:47:14 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:47:14 - Job submitted: job_id='663bac92d448b017e54f94ac' 2024-05-08 09:47:15 - QUEUED 2024-05-08 09:47:17 - RUNNING 2024-05-08 09:47:58 - COMPLETED 2024-05-08 09:48:01 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:47:14.96Z', 'queued_at_rfc3339nano': '2024-05-08T16:47:14.962Z', 'running_at_rfc3339nano': '2024-05-08T16:47:15.736Z', 'completed_at_rfc3339nano': '2024-05-08T16:47:56.304Z'} Energy: -400 Solution [400, 0, 0] Solution Value (should match energy) [-400.] Known ground state [-400.] With solution [400 0 0]

## Dynamic Range

Solution precision is dictated by the machine sensitivity and precision, including those of the photon-counting modules and optoelectronic devices, and shot-noise limited quantum measurements. Dirac-3 has a dynamic range of at least 23 dB. This means that Hamiltonian coefficients having an absolute value smaller than ~1/200 of the peak coefficient value may not be recognized and will be effectively treated as 0 when encoding the Hamiltonian. In some cases, the dynamic range can reach as high as 40 dB. However, it is not guaranteed.

During computing cycles, each qudit takes discrete numbers of levels, which is at least 200, but can reach 10,000, as limited by the 23 dB or 40 dB dynamic range. At the output, however, the optimal solutions may consist of variables that have fractional components, as a result of normalization to the summation constraint. Thus when the number of levels is 200, each qudit value will be a multiple of R/200.

Device sensitivity is not a controllable parameter and so the number of levels is not a part of problem formulation. More details on this are available in the Dirac-3 User Guide.

## Summation Constraint

The summation constraint is required to be some value between 1 and 10,000. This restriction is a property of Dirac-3.1 and not EQC in general. This constraint is helpful in some models and a formulation hurdle in others. In all formulations of TSP, for instance, the sum of a final solution is known beforehand. A Hamiltonian cycle (another one of those confusing terminologies that shows up when mathematicians and physicists work together) has exactly $n$ edges, where $n$ is the number of nodes in a graph. This means that solving a TSP with Dirac-3 requires setting the sum constraint equal to the number of nodes in the graph.

Other cases where the sparsity or sum of the solution is not known before solving can be solved with a couple of different approaches.

### Machine Slack Qudits

When formulating a model, it can be advantageous to add slack variables. To use this method when setting a summation constraint, formulate the Hamiltonian like normal, then determine a lower bound and an upper bound on the sum of the qudit values. Introduce an additional qudit into the formulated model which can sum to the upper bound minus the lower bound. This additional variable in the model will be ignored in the solution. See some of the more indepth examples, like Max Cut, to understand slack qudits in action.

### Minimizing a simple polynomial

Let $H(x)=-5x_1x_3-4x_2^2-10x_1-10x_3$. This polynomial itself has no lower bound, but as we put a summation constraint, it does. If $\sum_i x_i = R$, then $H(s)$ has a minimum value that changes negatively with $R$ and changes the cardinality of $x$. Below are the results for different $R$ values.

In [6]:

`h = np.array([[-10.],[ 0.],[-10.]], dtype=np.float32)J = np.array([[0., 0., -2.5],[0., -4., 0. ],[-2.5, 0., 0. ]], dtype=np.float32)for S in [2, 3, 4, 5, 6, 7]:print("**********************************")print(f"S={S}")response = sample_hamiltonian(h, J, S, 3, 0.1, q1)results = get_results(response)print("Status:", response["job_info"]["job_status"])results = get_results(response)print("Energy:", results["energies"][0])print("Solution")print(results["solutions"][0])print()`

Out [ ]:

********************************** S=2 2024-05-08 09:49:01 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:49:01 - Job submitted: job_id='663bacfdd448b017e54f94ad' 2024-05-08 09:49:01 - QUEUED 2024-05-08 09:49:04 - RUNNING 2024-05-08 09:49:55 - COMPLETED 2024-05-08 09:49:58 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:49:01.515Z', 'queued_at_rfc3339nano': '2024-05-08T16:49:01.517Z', 'running_at_rfc3339nano': '2024-05-08T16:49:01.823Z', 'completed_at_rfc3339nano': '2024-05-08T16:49:54.459Z'} Energy: -24.9997101 Solution [0.9924028, 0, 1.0075972] ********************************** S=3 2024-05-08 09:50:38 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:50:38 - Job submitted: job_id='663bad5ed448b017e54f94ae' 2024-05-08 09:50:38 - QUEUED 2024-05-08 09:50:41 - RUNNING 2024-05-08 09:51:32 - COMPLETED 2024-05-08 09:51:35 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:50:38.857Z', 'queued_at_rfc3339nano': '2024-05-08T16:50:38.859Z', 'running_at_rfc3339nano': '2024-05-08T16:50:39.525Z', 'completed_at_rfc3339nano': '2024-05-08T16:51:32.208Z'} Energy: -41.2500076 Solution [1.4997756, 0, 1.5002246] ********************************** S=4 2024-05-08 09:52:15 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:52:15 - Job submitted: job_id='663badbfd448b017e54f94af' 2024-05-08 09:52:15 - QUEUED 2024-05-08 09:52:18 - RUNNING 2024-05-08 09:53:12 - COMPLETED 2024-05-08 09:53:14 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:52:15.857Z', 'queued_at_rfc3339nano': '2024-05-08T16:52:15.858Z', 'running_at_rfc3339nano': '2024-05-08T16:52:16.329Z', 'completed_at_rfc3339nano': '2024-05-08T16:53:10.127Z'} Energy: -59.9980927 Solution [2.0195472, 0, 1.9804529] ********************************** S=5 2024-05-08 09:53:55 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:53:55 - Job submitted: job_id='663bae23d448b017e54f94b0' 2024-05-08 09:53:55 - QUEUED 2024-05-08 09:53:57 - RUNNING 2024-05-08 09:54:49 - COMPLETED 2024-05-08 09:54:51 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:53:55.363Z', 'queued_at_rfc3339nano': '2024-05-08T16:53:55.364Z', 'running_at_rfc3339nano': '2024-05-08T16:53:56.372Z', 'completed_at_rfc3339nano': '2024-05-08T16:54:47.923Z'} Energy: -81.2500153 Solution [2.5000925, 0, 2.499908] ********************************** S=6 2024-05-08 09:55:32 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:55:32 - Job submitted: job_id='663bae84d448b017e54f94b1' 2024-05-08 09:55:32 - QUEUED 2024-05-08 09:55:35 - RUNNING 2024-05-08 09:56:57 - COMPLETED 2024-05-08 09:56:59 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:55:32.437Z', 'queued_at_rfc3339nano': '2024-05-08T16:55:32.439Z', 'running_at_rfc3339nano': '2024-05-08T16:55:33.057Z', 'completed_at_rfc3339nano': '2024-05-08T16:56:55.554Z'} Energy: -105 Solution [3, 0, 3] ********************************** S=7 2024-05-08 09:57:40 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 09:57:40 - Job submitted: job_id='663baf04d448b017e54f94b2' 2024-05-08 09:57:40 - QUEUED 2024-05-08 09:57:43 - RUNNING 2024-05-08 09:58:34 - COMPLETED 2024-05-08 09:58:37 - Dirac allocation balance = 0 s (unmetered) Status: {'submitted_at_rfc3339nano': '2024-05-08T16:57:40.414Z', 'queued_at_rfc3339nano': '2024-05-08T16:57:40.416Z', 'running_at_rfc3339nano': '2024-05-08T16:57:40.618Z', 'completed_at_rfc3339nano': '2024-05-08T16:58:33.574Z'} Energy: -196 Solution [2e-07, 7, 2e-07]

For reference, here is a job response that is returned in JSON from the REST API

In [7]:

response

Out [7]:

{'job_info': {'job_id': '663baf04d448b017e54f94b2', 'job_submission': {'job_tags': ['qudit-tutorial'], 'problem_config': {'normalized_qudit_hamiltonian_optimization': {'hamiltonian_file_id': '663baf0498263204a36574f8'}}, 'device_config': {'dirac-3': {'num_samples': 1, 'relaxation_schedule': 3, 'solution_precision': 0.1, 'sum_constraint': 7}}}, 'job_status': {'submitted_at_rfc3339nano': '2024-05-08T16:57:40.414Z', 'queued_at_rfc3339nano': '2024-05-08T16:57:40.416Z', 'running_at_rfc3339nano': '2024-05-08T16:57:40.618Z', 'completed_at_rfc3339nano': '2024-05-08T16:58:33.574Z'}, 'job_result': {'file_id': '663baf3998263204a36574fa', 'device_usage_s': 13}}, 'status': 'COMPLETED', 'results': {'counts': [1], 'energies': [-196], 'solutions': [[2e-07, 7, 2e-07]], 'distilled_energies': [-196], 'distilled_solutions': [[0, 7, 0]]}}

## Degeneracy Demonstration

Dirac-3 will return various solutions to a problem with multiple solutions near the optimal value.

Let's see what happens with a clearly degenerate problem. Take the first example modified to give the same weight to all the linear terms. Each solution: `[S, 0, 0]`

, `[0, S, 0]`

and `[0, 0, S]`

give the same value for the Hamiltonian.

In [8]:

`h = np.array([[-1],[-1],[-1]], dtype=np.float32)J = np.array([[0, 1, 1],[1, 0, 1],[1, 1, 0]], dtype=np.float32)`

In [10]:

`super_test = {}energies_list = []for i in range(10):response = sample_hamiltonian(h, J, 400, 2, 0.1, q1)results = get_results(response)energies_list.append(results["energies"][0])solution = [round(v, 1) for v in results["solutions"][0]]solution = tuple(solution)if solution in super_test:super_test[solution] += 1else:super_test[solution] = 1print("Solution | Frequency")for solution in super_test:print(solution, " ", "*"*super_test[solution])labels = list(super_test.keys())sizes = [super_test[key] for key in labels]fig, ax = plt.subplots()patches, labels = ax.pie(sizes, labels=labels)`

Out [ ]:

2024-05-08 10:04:59 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:05:00 - Job submitted: job_id='663bb0bbd448b017e54f94b3' 2024-05-08 10:05:00 - QUEUED 2024-05-08 10:05:02 - RUNNING 2024-05-08 10:05:51 - COMPLETED 2024-05-08 10:05:54 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:06:34 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:06:34 - Job submitted: job_id='663bb11ad448b017e54f94b4' 2024-05-08 10:06:34 - QUEUED 2024-05-08 10:06:37 - RUNNING 2024-05-08 10:07:43 - COMPLETED 2024-05-08 10:07:46 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:08:27 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:08:27 - Job submitted: job_id='663bb18bd448b017e54f94b5' 2024-05-08 10:08:27 - QUEUED 2024-05-08 10:08:30 - RUNNING 2024-05-08 10:09:19 - COMPLETED 2024-05-08 10:09:21 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:10:02 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:10:02 - Job submitted: job_id='663bb1ead448b017e54f94b6' 2024-05-08 10:10:02 - QUEUED 2024-05-08 10:10:04 - RUNNING 2024-05-08 10:10:50 - COMPLETED 2024-05-08 10:10:53 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:11:33 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:11:33 - Job submitted: job_id='663bb245d448b017e54f94b7' 2024-05-08 10:11:33 - QUEUED 2024-05-08 10:11:36 - RUNNING 2024-05-08 10:12:22 - COMPLETED 2024-05-08 10:12:25 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:13:05 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:13:05 - Job submitted: job_id='663bb2a1d448b017e54f94b8' 2024-05-08 10:13:05 - QUEUED 2024-05-08 10:13:08 - RUNNING 2024-05-08 10:13:57 - COMPLETED 2024-05-08 10:13:59 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:14:39 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:14:40 - Job submitted: job_id='663bb300d448b017e54f94b9' 2024-05-08 10:14:40 - QUEUED 2024-05-08 10:14:42 - RUNNING 2024-05-08 10:15:31 - COMPLETED 2024-05-08 10:15:34 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:16:14 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:16:14 - Job submitted: job_id='663bb35ed448b017e54f94ba' 2024-05-08 10:16:14 - QUEUED 2024-05-08 10:16:17 - RUNNING 2024-05-08 10:17:03 - COMPLETED 2024-05-08 10:17:06 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:17:46 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:17:46 - Job submitted: job_id='663bb3bad448b017e54f94bb' 2024-05-08 10:17:46 - QUEUED 2024-05-08 10:18:25 - RUNNING 2024-05-08 10:19:14 - COMPLETED 2024-05-08 10:19:16 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:19:57 - Dirac allocation balance = 0 s (unmetered) 2024-05-08 10:19:57 - Job submitted: job_id='663bb43dd448b017e54f94bc' 2024-05-08 10:19:57 - QUEUED 2024-05-08 10:19:59 - RUNNING 2024-05-08 10:20:48 - COMPLETED 2024-05-08 10:20:51 - Dirac allocation balance = 0 s (unmetered) Solution | Frequency (0, 400, 0) *** (400, 0, 0) ***** (0.0, 0.0, 400) **

Out [ ]:

<Figure size 640x480 with 1 Axes>