Skip to content

Commit 83f3579

Browse files
Add NDArrays to cirq-google protos (#7032)
* Add NDArrays to cirq-google protos - This is a port of internal library to convert numpy ndarrays to proto and back for serialization. * build protos * Fix a bunch of CI issues. * Fix cover and mypy * Address comments and type naming. * Fix type --------- Co-authored-by: Pavol Juhas <[email protected]>
1 parent 21209df commit 83f3579

File tree

5 files changed

+1319
-0
lines changed

5 files changed

+1319
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Packed arrays store n-dimensional, homogeneous numeric data in a serialized
3+
* byte array, along with their shape and endianness. Their performance is
4+
* competitive with labrad and flatbuffer serialization/deserialization due to
5+
* the reduced scope of functionality (i.e. just arrays).
6+
*
7+
* Each packed type has:
8+
* - shape: a list of integers that mimic numpy's shape parameter. That is,
9+
* each element represents the length of a dimension. A 2-D n x m array would
10+
* have the shape [n, m].
11+
* - endianness: The endianness of encoding; client libraries are responsible
12+
* for correcting mismatched endianness. We highly recommend client
13+
* libraries to stick with consistent and, preferably, system-native
14+
* endianness. Note that unspecified endianness defaults to little endian;
15+
* this approximately reflects how protobufs are serialized on the wire.
16+
*
17+
* Note that different types are used to enforce typing in downstream data
18+
* data sources without inspecting the proto data. (For instance, enforcing
19+
* that a field stored as a proto is Complex128 requires only checking message
20+
* type and not the data.)
21+
*/
22+
syntax = "proto3";
23+
package cirq.google.api.v2;
24+
25+
option java_package = "com.google.cirq.google.api.v2";
26+
option java_outer_classname = "NDArrayProto";
27+
option java_multiple_files = true;
28+
29+
30+
enum Endianness {
31+
LITTLE_ENDIAN = 0;
32+
BIG_ENDIAN = 1;
33+
}
34+
35+
/*
36+
* Array of interleaved (real, imaginary) pairs, each of which are 64-bit floats.
37+
*/
38+
message Complex128Array {
39+
repeated uint32 shape = 1;
40+
Endianness endianness = 2;
41+
oneof data {
42+
bytes flat_bytes = 3;
43+
}
44+
}
45+
46+
/*
47+
* Array of interleaved (real, imaginary) pairs, each of which are 32-bit floats.
48+
*/
49+
message Complex64Array {
50+
repeated uint32 shape = 1;
51+
Endianness endianness = 2;
52+
oneof data {
53+
bytes flat_bytes = 3;
54+
}
55+
}
56+
57+
message Float16Array {
58+
repeated uint32 shape = 1;
59+
Endianness endianness = 2;
60+
oneof data {
61+
bytes flat_bytes = 3;
62+
}
63+
}
64+
65+
message Float32Array {
66+
repeated uint32 shape = 1;
67+
Endianness endianness = 2;
68+
oneof data {
69+
bytes flat_bytes = 3;
70+
}
71+
}
72+
73+
message Float64Array {
74+
repeated uint32 shape = 1;
75+
Endianness endianness = 2;
76+
oneof data {
77+
bytes flat_bytes = 3;
78+
}
79+
}
80+
81+
message Int64Array {
82+
repeated uint32 shape = 1;
83+
Endianness endianness = 2;
84+
oneof data {
85+
bytes flat_bytes = 3;
86+
}
87+
}
88+
89+
message Int32Array {
90+
repeated uint32 shape = 1;
91+
Endianness endianness = 2;
92+
oneof data {
93+
bytes flat_bytes = 3;
94+
}
95+
}
96+
97+
message Int16Array {
98+
repeated uint32 shape = 1;
99+
Endianness endianness = 2;
100+
oneof data {
101+
bytes flat_bytes = 3;
102+
}
103+
}
104+
105+
message Int8Array {
106+
repeated uint32 shape = 1;
107+
oneof data {
108+
bytes flat_bytes = 2;
109+
}
110+
}
111+
112+
message UInt8Array {
113+
repeated uint32 shape = 1;
114+
oneof data {
115+
bytes flat_bytes = 2;
116+
}
117+
}
118+
119+
/*
120+
* BitArrays represent arbitrary shape bit arrays.
121+
*
122+
* They are packed into bytes, in big-endian bit order and therefore will
123+
* consume ceil(product(shape) / 8) bytes.
124+
*
125+
* For example, say we have an array:
126+
* array = [0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0]
127+
* Where array[0] == 0, array[1] == 1, array[2] == 1, etc.
128+
*
129+
* It will get mapped into bytes as follows:
130+
* 0 1 1 0 1 0 0 0 : 1 0 1 0
131+
* [ byte 0 | byte 1 ]
132+
* 7 6 5 4 3 2 1 0 : 7 6 5 4 3 2 1 0 <-- bit index in byte
133+
* ^^^^^^^^^^^
134+
* unused bits will be zeroed out
135+
*
136+
*/
137+
message BitArray {
138+
repeated uint32 shape = 1;
139+
oneof data {
140+
bytes flat_bytes = 2;
141+
}
142+
}

0 commit comments

Comments
 (0)