Skip to content

Commit f54e03d

Browse files
fdmalonempharrigan
andauthored
THC prepare (#350)
* WIP. * Updates. * Refactor to use cirq bloqs. * Update docstring. * Finish conversion to cirq wrapped bloqs. * Remove old file. * Check alt keep values. * Delete extra tutorial for later PR. * Fix registers. * Fix missing import. * Update basic gates. * WIP. * Fix errors. * Revert toffoli change. * Tidy arithmetic t_complexity. * Updates. * Fix lint errors. * Fix lint and cirq-ft version issue. * Fixes. * Updates for cirq interop. * Fix decomposition. * Fix formatting. * Remove unused function. * Fix reprs to avoid data arrays being printed in the output. * Fix errors from merge. * Fix import. * Remove unused import. * Add issue link. * Fix trailing whitespace. * Bump cirq version for arithmetic #6313 and #6312. * Add arithmetic t_complexity calls to unit tests. * Address review comments. * Address review comments. * Fix generalizer for pauli gates. * Address review comments. * Use qualtran bloqs. * Remove unused code. * Fix CZ bloq. * [skip ci] WIP. * replace multi control pauli. * Correct classification. * Group and bloqs. * Better formatting for bloq counts. * Add issue link about removing code. * Fix lint and format errors. --------- Co-authored-by: Matthew Harrigan <mpharrigan@google.com>
1 parent 684c227 commit f54e03d

File tree

12 files changed

+977
-1
lines changed

12 files changed

+977
-1
lines changed

dev_tools/autogenerate-bloqs-notebooks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import qualtran.bloqs.basic_gates.toffoli_test
6060
import qualtran.bloqs.basic_gates.x_basis_test
6161
import qualtran.bloqs.basic_gates.z_basis_test
62+
import qualtran.bloqs.chemistry.thc_test
6263
import qualtran.bloqs.factoring.mod_exp
6364
import qualtran.bloqs.factoring.mod_exp_test
6465
import qualtran.bloqs.factoring.mod_mul_test
@@ -140,6 +141,15 @@
140141
],
141142
directory=f'{SOURCE_DIR}/bloqs/factoring',
142143
),
144+
NotebookSpec(
145+
title='Tensor Hypercontraction',
146+
module=qualtran.bloqs.chemistry.thc,
147+
gate_specs=[
148+
BloqNbSpec(qualtran.bloqs.chemistry.thc_test._make_uniform_superposition),
149+
BloqNbSpec(qualtran.bloqs.chemistry.thc_test._make_prepare),
150+
],
151+
directory=f'{SOURCE_DIR}/bloqs/chemistry',
152+
),
143153
]
144154

145155

qualtran/bloqs/arithmetic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def signature(self) -> Signature:
10371037
return Signature.build(x=self.bitsize, target=1)
10381038

10391039
def t_complexity(self) -> TComplexity:
1040-
return t_complexity(LessThanConstant(self.bitsize, val=self.val))
1040+
return t_complexity(LessThanConstant(self.bitsize, less_than_val=self.val))
10411041

10421042
def bloq_counts(
10431043
self, ssa: Optional['SympySymbolAllocator'] = None

qualtran/bloqs/arithmetic_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def test_add():
351351
q1 = bb.add_register('b', bitsize)
352352
a, b = bb.add(Add(bitsize), a=q0, b=q1)
353353
cbloq = bb.finalize(a=a, b=b)
354+
cbloq.t_complexity()
354355

355356

356357
@pytest.mark.parametrize('bitsize', [3])
@@ -402,6 +403,7 @@ def test_out_of_place_adder():
402403
q2 = bb.add_register('c', bitsize)
403404
a, b, c = bb.add(OutOfPlaceAdder(bitsize), a=q0, b=q1, c=q2)
404405
cbloq = bb.finalize(a=a, b=b, c=c)
406+
cbloq.t_complexity()
405407

406408

407409
def test_square():
@@ -410,6 +412,7 @@ def test_square():
410412
q0 = bb.add_register('a', bitsize)
411413
q0, q1 = bb.add(Square(bitsize), a=q0)
412414
cbloq = bb.finalize(a=q0, result=q1)
415+
cbloq.t_complexity()
413416

414417

415418
def test_sum_of_squares():
@@ -420,6 +423,7 @@ def test_sum_of_squares():
420423
inp, out = bb.add(SumOfSquares(bitsize, k), input=inp)
421424
cbloq = bb.finalize(input=inp, result=out)
422425
assert SumOfSquares(bitsize, k).signature[1].bitsize == 2 * bitsize + 2
426+
cbloq.t_complexity()
423427

424428

425429
def test_product():
@@ -430,6 +434,7 @@ def test_product():
430434
q1 = bb.add_register('b', mbits)
431435
q0, q1, q2 = bb.add(Product(bitsize, mbits), a=q0, b=q1)
432436
cbloq = bb.finalize(a=q0, b=q1, result=q2)
437+
cbloq.t_complexity()
433438

434439

435440
def test_scale_int_by_real():
@@ -438,6 +443,7 @@ def test_scale_int_by_real():
438443
q1 = bb.add_register('b', 8)
439444
q0, q1, q2 = bb.add(ScaleIntByReal(15, 8), real_in=q0, int_in=q1)
440445
cbloq = bb.finalize(a=q0, b=q1, result=q2)
446+
cbloq.t_complexity()
441447

442448

443449
def test_multiply_two_reals():
@@ -446,6 +452,7 @@ def test_multiply_two_reals():
446452
q1 = bb.add_register('b', 15)
447453
q0, q1, q2 = bb.add(MultiplyTwoReals(15), a=q0, b=q1)
448454
cbloq = bb.finalize(a=q0, b=q1, result=q2)
455+
cbloq.t_complexity()
449456

450457

451458
def test_square_real_number():
@@ -464,6 +471,7 @@ def test_greater_than():
464471
anc = bb.add_register('result', 1)
465472
q0, q1, anc = bb.add(GreaterThan(bitsize), a=q0, b=q1, target=anc)
466473
cbloq = bb.finalize(a=q0, b=q1, result=anc)
474+
cbloq.t_complexity()
467475

468476

469477
def test_greater_than_constant():
@@ -473,6 +481,7 @@ def test_greater_than_constant():
473481
anc = bb.add_register('result', 1)
474482
q0, anc = bb.add(GreaterThanConstant(bitsize, 17), x=q0, target=anc)
475483
cbloq = bb.finalize(x=q0, result=anc)
484+
cbloq.t_complexity()
476485

477486

478487
def test_equals_a_constant():
@@ -482,6 +491,7 @@ def test_equals_a_constant():
482491
anc = bb.add_register('result', 1)
483492
q0, anc = bb.add(EqualsAConstant(bitsize, 17), x=q0, target=anc)
484493
cbloq = bb.finalize(x=q0, result=anc)
494+
cbloq.t_complexity()
485495

486496

487497
def test_to_contiguous_index():
@@ -492,6 +502,7 @@ def test_to_contiguous_index():
492502
out = bb.add_register('s', 1)
493503
q0, q1, out = bb.add(ToContiguousIndex(bitsize, 2 * bitsize), mu=q0, nu=q1, s=out)
494504
cbloq = bb.finalize(mu=q0, nu=q1, s=out)
505+
cbloq.t_complexity()
495506

496507

497508
def test_arithmetic_notebook():

qualtran/bloqs/basic_gates/hadamard.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import numpy as np
1919
from attrs import frozen
20+
from cirq_ft import TComplexity
2021

2122
from qualtran import Bloq, Signature, SoquetT
2223

@@ -74,3 +75,6 @@ def as_cirq_op(
7475

7576
(q,) = q
7677
return cirq.H(q), {'q': np.array([q])}
78+
79+
def t_complexity(self):
80+
return TComplexity(clifford=1)

qualtran/bloqs/basic_gates/rotation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import cirq
2929

3030
from qualtran.cirq_interop import CirqQuregT
31+
from qualtran.resource_counting import SympySymbolAllocator
3132

3233

3334
@frozen

qualtran/bloqs/basic_gates/toffoli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from typing import Dict, Optional, Set, Tuple, TYPE_CHECKING, Union
1717

1818
from attrs import frozen
19+
from cirq_ft import TComplexity
1920

2021
from qualtran import Bloq, Register, Signature
2122
from qualtran.bloqs.basic_gates import TGate
23+
from qualtran.resource_counting import SympySymbolAllocator
2224

2325
if TYPE_CHECKING:
2426
import cirq
@@ -50,6 +52,9 @@ def signature(self) -> Signature:
5052
def bloq_counts(self, ssa: Optional['SympySymbolAllocator'] = None) -> Set['BloqCountT']:
5153
return {(4, TGate())}
5254

55+
def t_complexity(self):
56+
return TComplexity(t=4)
57+
5358
def on_classical_vals(
5459
self, ctrl: 'ClassicalValT', target: 'ClassicalValT'
5560
) -> Dict[str, 'ClassicalValT']:

qualtran/bloqs/basic_gates/x_basis.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import quimb.tensor as qtn
2020
from attrs import frozen
21+
from cirq_ft import TComplexity
2122

2223
from qualtran import Bloq, Register, Side, Signature, SoquetT
2324

@@ -177,3 +178,6 @@ def as_cirq_op(
177178

178179
(q,) = q
179180
return cirq.X(q), {'q': [q]}
181+
182+
def t_complexity(self):
183+
return TComplexity(clifford=1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

0 commit comments

Comments
 (0)