microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
sccarda/BlochLearning

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

library/std/src/Std/OpenQASM/Builtin.qs

120lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4/// This file defines the bultin functions used in the OpenQASM runtime.
5/// It is an internal implementation detail for OpenQASM compilation
6/// and is not intended for use outside of this context.
7
8/// OpenQASM only supports up to seven dimensions,
9/// therefore, we only need these seven `sizeof` functions.
10export sizeof_1, sizeof_2, sizeof_3, sizeof_4, sizeof_5, sizeof_6, sizeof_7;
11
12/// Function to handle 1-dimensional arrays passed to `sizeof` in OpenQASM.
13///
14/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
15/// It returns the length of the requested dimension.
16/// Fails if the requested dimension is greater than the number of dimensions
17/// in the array.
18function sizeof_1<'T>(array : 'T[], dim : Int) : Int {
19 if dim == 0 {
20 Length(array)
21 } else {
22 fail $"sizeof error: requested dimension {dim} but the array has 1 dimension";
23 }
24}
25
26/// Function to handle 2-dimensional arrays passed to `sizeof` in OpenQASM.
27///
28/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
29/// It returns the length of the requested dimension.
30/// Fails if the requested dimension is greater than the number of dimensions
31/// in the array.
32function sizeof_2<'T>(array : 'T[][], dim : Int) : Int {
33 if dim == 0 {
34 Length(array)
35 } elif dim < 2 {
36 sizeof_1(array[0], dim - 1)
37 } else {
38 fail $"sizeof error: requested dimension {dim} but the array has 2 dimensions";
39 }
40}
41
42/// Function to handle 3-dimensional arrays passed to `sizeof` in OpenQASM.
43///
44/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
45/// It returns the length of the requested dimension.
46/// Fails if the requested dimension is greater than the number of dimensions
47/// in the array.
48function sizeof_3<'T>(array : 'T[][][], dim : Int) : Int {
49 if dim == 0 {
50 Length(array)
51 } elif dim < 3 {
52 sizeof_2(array[0], dim - 1)
53 } else {
54 fail $"sizeof error: requested dimension {dim} but the array has 3 dimensions";
55 }
56}
57
58/// Function to handle 4-dimensional arrays passed to `sizeof` in OpenQASM.
59///
60/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
61/// It returns the length of the requested dimension.
62/// Fails if the requested dimension is greater than the number of dimensions
63/// in the array.
64function sizeof_4<'T>(array : 'T[][][][], dim : Int) : Int {
65 if dim == 0 {
66 Length(array)
67 } elif dim < 4 {
68 sizeof_3(array[0], dim - 1)
69 } else {
70 fail $"sizeof error: requested dimension {dim} but the array has 4 dimensions";
71 }
72}
73
74/// Function to handle 5-dimensional arrays passed to `sizeof` in OpenQASM.
75///
76/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
77/// It returns the length of the requested dimension.
78/// Fails if the requested dimension is greater than the number of dimensions
79/// in the array.
80function sizeof_5<'T>(array : 'T[][][][][], dim : Int) : Int {
81 if dim == 0 {
82 Length(array)
83 } elif dim < 5 {
84 sizeof_4(array[0], dim - 1)
85 } else {
86 fail $"sizeof error: requested dimension {dim} but the array has 5 dimensions";
87 }
88}
89
90/// Function to handle 6-dimensional arrays passed to `sizeof` in OpenQASM.
91///
92/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
93/// It returns the length of the requested dimension.
94/// Fails if the requested dimension is greater than the number of dimensions
95/// in the array.
96function sizeof_6<'T>(array : 'T[][][][][][], dim : Int) : Int {
97 if dim == 0 {
98 Length(array)
99 } elif dim < 6 {
100 sizeof_5(array[0], dim - 1)
101 } else {
102 fail $"sizeof error: requested dimension {dim} but the array has 6 dimensions";
103 }
104}
105
106/// Function to handle 7-dimensional arrays passed to `sizeof` in OpenQASM.
107///
108/// It takes two arguments, an array `array` and a zero-based dimension `dim`.
109/// It returns the length of the requested dimension.
110/// Fails if the requested dimension is greater than the number of dimensions
111/// in the array.
112function sizeof_7<'T>(array : 'T[][][][][][][], dim : Int) : Int {
113 if dim == 0 {
114 Length(array)
115 } elif dim < 7 {
116 sizeof_6(array[0], dim - 1)
117 } else {
118 fail $"sizeof error: requested dimension {dim} but the array has 7 dimensions";
119 }
120}
121