Skip to content

Commit

Permalink
[style] Only use "cbloq" for composite bloqs (#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan authored Aug 21, 2024
1 parent 84324f9 commit 0559dfd
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions qualtran/_infra/controlled_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ def test_notebook():


def _verify_ctrl_tensor_for_unitary(ctrl_spec: CtrlSpec, bloq: Bloq, gate: cirq.Gate):
cbloq = Controlled(bloq, ctrl_spec)
ctrl_bloq = Controlled(bloq, ctrl_spec)
cgate = cirq.ControlledGate(gate, control_values=ctrl_spec.to_cirq_cv())
np.testing.assert_allclose(cbloq.tensor_contract(), cirq.unitary(cgate), atol=1e-8)
np.testing.assert_allclose(ctrl_bloq.tensor_contract(), cirq.unitary(cgate), atol=1e-8)


interesting_ctrl_specs = [
Expand All @@ -368,15 +368,15 @@ def test_controlled_tensor_for_unitary(ctrl_spec: CtrlSpec):
def test_controlled_tensor_without_decompose():
ctrl_spec = CtrlSpec()
bloq = TwoBitCSwap()
cbloq = Controlled(bloq, ctrl_spec)
ctrl_bloq = Controlled(bloq, ctrl_spec)
cgate = cirq.ControlledGate(cirq.CSWAP, control_values=ctrl_spec.to_cirq_cv())

tn = cbloq_to_quimb(cbloq.as_composite_bloq())
tn = cbloq_to_quimb(ctrl_bloq.as_composite_bloq())
# pylint: disable=unbalanced-tuple-unpacking
right, left = get_right_and_left_inds(tn, cbloq.signature)
right, left = get_right_and_left_inds(tn, ctrl_bloq.signature)
# pylint: enable=unbalanced-tuple-unpacking
np.testing.assert_allclose(tn.to_dense(right, left), cirq.unitary(cgate), atol=1e-8)
np.testing.assert_allclose(cbloq.tensor_contract(), cirq.unitary(cgate), atol=1e-8)
np.testing.assert_allclose(ctrl_bloq.tensor_contract(), cirq.unitary(cgate), atol=1e-8)


def test_controlled_global_phase_tensor():
Expand All @@ -402,30 +402,30 @@ def signature(self) -> 'Signature':

def build_composite_bloq(self, bb: 'BloqBuilder') -> Dict[str, 'SoquetT']:
one_or_zero = [ZeroState(), OneState()]
cbloq = Controlled(And(*self.and_ctrl), ctrl_spec=self.ctrl_spec)
ctrl_bloq = Controlled(And(*self.and_ctrl), ctrl_spec=self.ctrl_spec)

ctrl_soqs = {}
for reg, cvs in zip(cbloq.ctrl_regs, self.ctrl_spec.cvs):
for reg, cvs in zip(ctrl_bloq.ctrl_regs, self.ctrl_spec.cvs):
soqs = np.empty(shape=reg.shape, dtype=object)
for idx in reg.all_idxs():
soqs[idx] = bb.add(IntState(val=cvs[idx], bitsize=reg.dtype.num_qubits))
ctrl_soqs[reg.name] = soqs

and_ctrl = [bb.add(one_or_zero[cv]) for cv in self.and_ctrl]

ctrl_soqs = bb.add_d(cbloq, **ctrl_soqs, ctrl=and_ctrl)
ctrl_soqs = bb.add_d(ctrl_bloq, **ctrl_soqs, ctrl=and_ctrl)
out_soqs = np.asarray([*ctrl_soqs.pop('ctrl'), ctrl_soqs.pop('target')]) # type: ignore[misc]

for reg, cvs in zip(cbloq.ctrl_regs, self.ctrl_spec.cvs):
for reg, cvs in zip(ctrl_bloq.ctrl_regs, self.ctrl_spec.cvs):
for idx in reg.all_idxs():
ctrl_soq = np.asarray(ctrl_soqs[reg.name])[idx]
bb.add(IntEffect(val=cvs[idx], bitsize=reg.dtype.num_qubits), val=ctrl_soq)
return {'x': out_soqs}


def _verify_ctrl_tensor_for_and(ctrl_spec: CtrlSpec, and_ctrl: Tuple[int, int]):
cbloq = TestCtrlStatePrepAnd(ctrl_spec, and_ctrl)
bloq_tensor = cbloq.tensor_contract()
bloq = TestCtrlStatePrepAnd(ctrl_spec, and_ctrl)
bloq_tensor = bloq.tensor_contract()
cirq_state_vector = GateHelper(And(*and_ctrl)).circuit.final_state_vector(
initial_state=and_ctrl + (0,)
)
Expand Down

0 comments on commit 0559dfd

Please sign in to comment.