22
33This style guide for Gemini outlines the coding conventions for this project.
44
5- ## General guidance
5+ ## Introduction and goals
66
7- ### Overall principles
7+ ### Overall goals
88
99* _ Readability_ : Code must be easy for humans to understand. Prefer clarity
1010 over cleverness. Write elegant, well-structured code.
@@ -16,39 +16,68 @@ This style guide for Gemini outlines the coding conventions for this project.
1616
1717* _ Performance_ : While readability is paramount, code should be efficient.
1818
19- ### Overall development approach
19+ ### Overall software engineering principles
2020
21- * Code should be modular and adhere to language idioms and best practices.
21+ * Write modular code that follows language idioms and best practices.
2222
23- * Data obtained from the user or read from a file should be validated.
24- Identify and guard against potential vulnerabilities in data handling or
25- input validation.
23+ * Prioritize numerical stability and accuracy. Keep in mind the limitations of
24+ floating-point arithmetic (e.g., float, double).
2625
27- * When new functions, classes, and files are introduced, they should also
28- have corresponding tests. Existing tests must continue to pass (or be
29- updated) when changes are introduced, and code should be covered by tests.
26+ * Strive for performance through memory efficiency. Watch out for bottlenecks
27+ due to memory access. Design data structures and algorithms to promote data
28+ locality. Access memory sequentially (e.g., iterating through a
29+ ` std::vector ` ) to maximize cache hits.
3030
31- * Test coverage must be high. We don't require 100% coverage, but any
32- uncovered code must be annotated appropriate directives (for example,
33- ` # pragma: no cover ` in Python) .
31+ * Design for vectorization and parallelism. Structure loops and data access
32+ patterns to be friendly to compiler vectorization (SIMD). Avoid complex
33+ control flow (if/else) inside tight loops where possible .
3434
35- * Tests must be independent and must not rely on the state of other tests.
36- Setup and teardown functions should be used to create a clean environment
37- for each test run. External dependencies (e.g., databases, network services,
38- file system) must be mocked to ensure the test is isolated to the unit under
39- test.
35+ * Validate all user and file-based data to guard against security
36+ vulnerabilities.
4037
41- * Make sure to cover edge cases: write tests for invalid inputs, null values,
42- empty arrays, zero values, and off-by-one errors .
38+ * New code requires new tests. Ensure existing tests continue to pass or are
39+ updated when making changes .
4340
44- * Use asserts in tests intelligently. Test assertions should be specific:
45- instead of just asserting ` true ` , assert that a specific value equals an
46- expected value. Provide meaningful failure messages.
41+ * Thoroughly test for edge cases, including invalid inputs, null values, empty
42+ arrays, and off-by-one errors.
4743
48- ### Overall code format conventions
44+ * Keep tests independent. Use setup and teardown functions for clean
45+ environments and mock all external dependencies.
4946
50- Quantum AI projects generally follows Google coding conventions, with a few
51- changes. The following Google style guides are the starting points:
47+ ## Development workflow
48+
49+ ### Before you start
50+
51+ * ** CRITICAL** : before changing any file, first ask the user if they want to
52+ create a git branch for the work you are about to do.
53+
54+ ### File naming conventions
55+
56+ * Regular file names should be in all lower case, using underscores as word
57+ separators as needed. The names should indicate the purpose of the code in
58+ the file, while also be kept as short as possible without compromising
59+ understandability.
60+
61+ * Test files are usually named after the file they test but with a name
62+ ending in ` _test.py ` or ` _test.cc ` . For example, ` something.py ` would have
63+ tests in a file named ` something_test.py ` .
64+
65+ ### File structure conventions
66+
67+ * Files must end with a final newline, unless they are special files that do
68+ not normally have ending newlines.
69+
70+ ### Git commit conventions
71+
72+ * Use ` git commit ` to commit changes to files as you work. Each commit should
73+ encompass a subportion of your work that is conceptually related.
74+
75+ * Each commit must have a title and a description.
76+
77+ ### Code style conventions
78+
79+ Follow these Google coding conventions, with some project-specific conventions
80+ noted below.
5281
5382* [ Google C++ Style Guide] (
5483 https://google.github.io/styleguide/cppguide.html )
@@ -63,8 +92,7 @@ changes. The following Google style guides are the starting points:
6392 https://google.github.io/styleguide/shellguide.html )
6493
6594To learn this project's conventions for line length, indentation, and other
66- details of coding style, please inspect the following configuration files (if
67- present at the top level of this project repository):
95+ details of coding style, please inspect the following configuration files:
6896
6997* [ ` .editorconfig ` ] ( ../.editorconfig ) for basic code editor configuration
7098 (e.g., indentation and line length) specified using the
@@ -83,14 +111,14 @@ present at the top level of this project repository):
83111
84112* [ ` .yamllint.yaml ` ] ( ../.yamllint.yaml ) for YAML files.
85113
86- ### Overall code comment conventions
114+ ### Code comment conventions
87115
88- Every source file must begin with a header comment with the copyright and
89- license. We use the Apache 2.0 license. Here is an example of the required file
90- header for a Python language code file :
116+ Every source code file longer than 2 lines must begin with a header comment with
117+ the copyright and license. We use the Apache 2.0 license. Here is an example for
118+ Python code:
91119
92120``` python
93- # Copyright 2025 Google LLC
121+ # Copyright 2026 Google LLC
94122#
95123# Licensed under the Apache License, Version 2.0 (the "License");
96124# you may not use this file except in compliance with the License.
@@ -123,6 +151,28 @@ For comments in other parts of the files, follow these guidelines:
123151
124152This section outlines coding conventions for Python code in this project.
125153
154+ ### Setting up a Python environment for development
155+
156+ To set up a Python development environment, do the following:
157+
158+ ``` shell
159+ python3 -m venv venv
160+ source venv/bin/activate
161+ pip install -U pip
162+ ```
163+
164+ If there is a ` dev-requirements.txt ` file in the directory, run
165+
166+ ``` shell
167+ pip install -r requirements.txt -r dev-requirements.txt
168+ ```
169+
170+ otherwise, run
171+
172+ ``` shell
173+ pip install -r requirements.txt --group dev
174+ ```
175+
126176### Python naming conventions
127177
128178This project follows some nomenclature rules and conventions in order to
@@ -137,7 +187,7 @@ naming, we can reduce cognitive load on human users and developers.
137187
138188* _ Functions_ : Use lowercase with underscores (snake_case). Examples:
139189 ` calculate_total() ` , ` process_data() ` . Internal functions are prefixed with
140- an understore (` _ ` ).
190+ an underscore (` _ ` ).
141191
142192* _ Classes_ : Use CapWords (CamelCase). Examples: ` UserManager ` ,
143193 ` PaymentProcessor ` .
@@ -164,7 +214,7 @@ naming, we can reduce cognitive load on human users and developers.
164214 amplitudes. If a function expects a NumPy array of amplitudes, its type
165215 annotation should be ` np.ndarray ` .
166216
167- ### Python docstrings and documentation
217+ ### Python docstrings and documentation
168218
169219This project uses [ Google style doc strings] (
170220http://google.github.io/styleguide/pyguide.html#381-docstrings ) with a Markdown
@@ -212,7 +262,8 @@ def some_function(a: int, b: str) -> float:
212262### Python formatting
213263
214264Note: the Python code uses 88-column line widths, which is the default used by
215- code the formatters ` black ` and ` flynt ` . (The C++ files use 80.)
265+ code the formatter ` black ` but not the [ Google Python Style Guide] (
266+ https://google.github.io/styleguide/pyguide.html ). (The latter use 80.)
216267
217268The following programs can be used to perform some automated formatting.
218269
@@ -236,7 +287,7 @@ where possible, especially on public classes and functions to serve as
236287documentation, but also on internal code so that the ` mypy ` type checker can
237288help catch coding errors.
238289
239- ### Python linting
290+ ### Linting Python code
240291
241292Python files:
242293
@@ -246,7 +297,7 @@ Python files:
246297
247298* ` pylint -j0 . ` will run ` pylint ` on all Python files.
248299
249- ### Python testing
300+ ### Testing Python code
250301
251302Unit and integration tests can be run for the Python portions of this project
252303using the following command:
@@ -279,7 +330,7 @@ This section outlines coding conventions for C++ code in this project.
279330 * A computational basis state (say, $|0000\rangle$) is typically
280331 referred to as a ` bitstring ` .
281332
282- ### C++ formatting
333+ ### Formatting C++ code
283334
284335Note: the C++ code files use 80-column line widths. (The Python files use 88.)
285336
@@ -288,7 +339,7 @@ Note: the C++ code files use 80-column line widths. (The Python files use 88.)
288339
289340* ` clang-format -i PATH/TO/FILE ` will reformat the file.
290341
291- #### C++ testing
342+ #### Testing C++ code
292343
293344If you only want to run tests for the core C++ libraries, use this command:
294345
@@ -299,16 +350,22 @@ bazel test tests:all
299350To build tests without running them, instead use:
300351
301352``` shell
302- bzel build tests:all
353+ bazel build --config=verbose tests:all
303354```
304355
305356## Shell script-specific guidance
306357
307358Shell scripts should use Bash.
308359
309- ### Shell script formatting
360+ ### Formatting shell scripts
310361
311362Use the [ Google Shell Style Guide] (
312363https://google.github.io/styleguide/shellguide.html ) with the following changes:
313364
314365* Use indentation of 4 spaces, not 2.
366+
367+ ## TOML file-specific guidance
368+
369+ ### Formatting ` .toml ` files
370+
371+ We use indentation of 4 spaces, not 2.
0 commit comments