Skip to content

Commit 6c4871e

Browse files
committed
docs: update docs for repeated structure
1 parent 1979a2b commit 6c4871e

File tree

4 files changed

+135
-8
lines changed

4 files changed

+135
-8
lines changed

docs/examples/qpe.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
description: Program representing QPE using geometric sequence
2+
input:
3+
program:
4+
children:
5+
- name: Hadamards
6+
ports:
7+
- direction: through
8+
name: register
9+
size: N
10+
- name: Evolution
11+
ports:
12+
- direction: input
13+
name: result_in
14+
size: bits_of_precision
15+
- direction: output
16+
name: result_out
17+
size: N
18+
- direction: input
19+
name: psi_in
20+
size: None
21+
- direction: output
22+
name: psi_out
23+
size: None
24+
children:
25+
- name: U
26+
ports:
27+
- direction: through
28+
name: result
29+
size: bits_of_precision
30+
- direction: through
31+
name: psi
32+
size: N
33+
resources:
34+
- name: T_gates
35+
type: additive
36+
value: N**2
37+
connections:
38+
- source: result_in
39+
target: U.result
40+
- source: U.result
41+
target: result_out
42+
- source: psi_in
43+
target: U.psi
44+
- source: U.psi
45+
target: psi_out
46+
repetition:
47+
count: bits_of_precision
48+
sequence:
49+
type: geometric
50+
ratio: 1
51+
- name: Inverse_QFT
52+
ports:
53+
- direction: through
54+
name: register
55+
size: N
56+
connections:
57+
- source: result_in
58+
target: Hadamards.register
59+
- source: Hadamards.register
60+
target: Evolution.result_in
61+
- source: Evolution.result_out
62+
target: Inverse_QFT.register
63+
- source: Inverse_QFT.register
64+
target: result_out
65+
- source: psi_in
66+
target: Evolution.psi_in
67+
- source: Evolution.psi_out
68+
target: psi_out
69+
name: QPE
70+
ports:
71+
- direction: input
72+
name: result_in
73+
size: ceil(log2(1/eps))
74+
- direction: output
75+
name: result_out
76+
size: ceil(log2(1/eps))
77+
- direction: input
78+
name: psi_in
79+
size: M
80+
- direction: output
81+
name: psi_out
82+
size: M
83+
84+
version: v1

docs/format.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,45 @@ beginning looks as follows:
153153
```yaml
154154
--8<-- "basic_program_concise.yaml"
155155
```
156+
157+
158+
### Repetitions
159+
160+
On top of the basic fileds listed above, one can also write a QREF routine which contains repetitions.
161+
162+
This can be added with `repetition` field:
163+
164+
```yaml
165+
repetition:
166+
count: ceil(1/eps)
167+
sequence:
168+
type: constant
169+
multiplier: 1
170+
```
171+
172+
`repetition` consists of two parts:
173+
174+
- `count` – defines how many times the child of the this routine should be repeated.
175+
- `sequence` – defines how the costs for the repetition will be aggregated. Each `sequence` has a field `type` which defines the type of the sequence. Depending on the type there are extra fields, summarized in the table below.
176+
177+
178+
There are 5 different sequences that one can currently use in QREF:
179+
180+
181+
| <div style="width:7em">Sequence type</div> | <div style="width:8em">Additional fields</div> | Description | Example |
182+
|-------|-------|-------|-------|
183+
| `constant`| `multiplier` | In each iteration child is repeated `multiplier` number of times. | Trotterization |
184+
| `arithmetic`| `difference`, `initial_term` | Iteration starts from `initial_term` repetitions of a child and then we increase the of repetitions by `difference` in every iteration. | QFT |
185+
| `geometric` | `ratio` | In each iteration number of repetitions is multiplied by `ratio`, starts for 1 repetition in the first iteartion. | QPE |
186+
| `closed_form` | `sum`, `prod`, `num_terms_symbol` | This can be used for cases, where we know the closed-form expression for the total cost of the routine given the number of repetitions is defined `num_terms_symbol`. `sum` is an expression for additive resources and `prod` is for multiplicative. | Any |
187+
| `custom` | `term_expression`, `iterator_symbol` | This can be used in case where we don't know the formula for closed form, but we do know the formula for each term, which is defined using `term_expression`, and we use `iterator_symbol` to denote the iterator. | Any |
188+
189+
190+
This representation abstracts out certain implementation details. Consider implementation of QPE using geometric sequence below. The child `U` of routine `Evolution` has two ports: `result` and `psi`, the with sizes `bits_of_precision` and `N`. Even though in the executable implementation each next controlled `U^2^i` only acts on one control qubit from the `result` register, there's currently no way of expressing it in QREF.
191+
192+
=== "YAML"
193+
194+
```yaml
195+
--8<-- "qpe.yaml"
196+
```
197+

src/qref/schema_v1.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
BaseModel,
2525
BeforeValidator,
2626
ConfigDict,
27+
Field,
2728
StringConstraints,
2829
model_validator,
2930
)
@@ -230,10 +231,10 @@ class RoutineV1(BaseModel):
230231
"""
231232

232233
name: _Name
233-
children: Annotated[NamedList[RoutineV1], _name_sorter] = NamedList["RoutineV1"]()
234+
children: Annotated[NamedList[RoutineV1], _name_sorter] = Field(default_factory=NamedList)
234235
type: str | None = None
235-
ports: Annotated[NamedList[PortV1], _name_sorter] = NamedList[PortV1]()
236-
resources: Annotated[NamedList[ResourceV1], _name_sorter] = NamedList[ResourceV1]()
236+
ports: Annotated[NamedList[PortV1], _name_sorter] = Field(default_factory=NamedList)
237+
resources: Annotated[NamedList[ResourceV1], _name_sorter] = Field(default_factory=NamedList)
237238
connections: Annotated[list[Annotated[ConnectionV1, _connection_parser]], _source_sorter] = []
238239
input_params: list[_OptionallyMultiNamespacedName] = []
239240
local_variables: dict[str, str] = {}

tests/qref/data/valid_programs/programs_with_repetitions/repetition_2_geometric.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ input:
1111
ports:
1212
- direction: input
1313
name: result_in
14-
size: N
14+
size: bits_of_precision
1515
- direction: output
1616
name: result_out
1717
size: N
1818
- direction: input
1919
name: psi_in
20-
size: M
20+
size: None
2121
- direction: output
2222
name: psi_out
23-
size: M
23+
size: None
2424
children:
2525
- name: U
2626
ports:
2727
- direction: through
2828
name: result
29-
size: N
29+
size: bits_of_precision
3030
- direction: through
3131
name: psi
3232
size: N
@@ -44,7 +44,7 @@ input:
4444
- source: U.psi
4545
target: psi_out
4646
repetition:
47-
count: N
47+
count: bits_of_precision
4848
sequence:
4949
type: geometric
5050
ratio: 1

0 commit comments

Comments
 (0)