Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
eliottrosenberg authored Sep 19, 2023
2 parents b4ce5db + d805d82 commit f4f3b22
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 55 deletions.
16 changes: 8 additions & 8 deletions cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,14 +1465,14 @@ def concat_ragged(
Beware that this method is *not* associative. For example:
>>> a, b = cirq.LineQubit.range(2)
>>> A = cirq.Circuit(cirq.H(a))
>>> B = cirq.Circuit(cirq.H(b))
>>> f = cirq.Circuit.concat_ragged
>>> f(f(A, B), A) == f(A, f(B, A))
False
>>> len(f(f(f(A, B), A), B)) == len(f(f(A, f(B, A)), B))
False
>>> a, b = cirq.LineQubit.range(2)
>>> A = cirq.Circuit(cirq.H(a))
>>> B = cirq.Circuit(cirq.H(b))
>>> f = cirq.Circuit.concat_ragged
>>> f(f(A, B), A) == f(A, f(B, A))
False
>>> len(f(f(f(A, B), A), B)) == len(f(f(A, f(B, A)), B))
False
Args:
*circuits: The circuits to concatenate.
Expand Down
78 changes: 40 additions & 38 deletions cirq-core/cirq/interop/quirk/url_to_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,40 @@ def quirk_url_to_circuit(
a billion laughs attack in the form of nested custom gates.
Examples:
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["H"],["•","X"]]}'
... ))
0: ───H───@───
1: ───────X───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["H"],["•","X"]]}',
... qubits=[cirq.NamedQubit('Alice'), cirq.NamedQubit('Bob')]
... ))
Alice: ───H───@───
Bob: ─────────X───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["iswap"]]}',
... extra_cell_makers={'iswap': cirq.ISWAP}))
0: ───iSwap───
1: ───iSwap───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["iswap"]]}',
... extra_cell_makers=[
... cirq.interop.quirk.cells.CellMaker(
... identifier='iswap',
... size=2,
... maker=lambda args: cirq.ISWAP(*args.qubits))
... ]))
0: ───iSwap───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["H"],["•","X"]]}'
... ))
0: ───H───@───
1: ───iSwap───
1: ───────X───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["H"],["•","X"]]}',
... qubits=[cirq.NamedQubit('Alice'), cirq.NamedQubit('Bob')]
... ))
Alice: ───H───@───
Bob: ─────────X───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["iswap"]]}',
... extra_cell_makers={'iswap': cirq.ISWAP}))
0: ───iSwap───
1: ───iSwap───
>>> print(cirq.quirk_url_to_circuit(
... 'http://algassert.com/quirk#circuit={"cols":[["iswap"]]}',
... extra_cell_makers=[
... cirq.interop.quirk.cells.CellMaker(
... identifier='iswap',
... size=2,
... maker=lambda args: cirq.ISWAP(*args.qubits))
... ]))
0: ───iSwap───
1: ───iSwap───
Returns:
The parsed circuit.
Expand Down Expand Up @@ -172,12 +173,13 @@ def quirk_json_to_circuit(
a billion laughs attack in the form of nested custom gates.
Examples:
>>> print(cirq.quirk_json_to_circuit(
... {"cols":[["H"], ["•", "X"]]}
... ))
0: ───H───@───
1: ───────X───
>>> print(cirq.quirk_json_to_circuit(
... {"cols":[["H"], ["•", "X"]]}
... ))
0: ───H───@───
1: ───────X───
Returns:
The parsed circuit.
Expand Down
15 changes: 9 additions & 6 deletions cirq-core/cirq/protocols/apply_channel_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ class ApplyChannelArgs:
r"""Arguments for efficiently performing a channel.
A channel performs the mapping
$$
X \rightarrow \sum_i A_i X A_i^\dagger
$$
$$
X \rightarrow \sum_i A_i X A_i^\dagger
$$
for operators $A_i$ that satisfy the normalization condition
$$
\sum_i A_i^\dagger A_i = I.
$$
$$
\sum_i A_i^\dagger A_i = I.
$$
The receiving object is expected to mutate `target_tensor` so that it
contains the density matrix after multiplication, and then return
Expand Down
15 changes: 13 additions & 2 deletions cirq-google/cirq_google/ops/internal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
self.gate_module = gate_module
self.gate_name = gate_name
self._num_qubits = num_qubits
self.gate_args = {arg: val for arg, val in kwargs.items()}
self.gate_args = kwargs

def _num_qubits_(self) -> int:
return self._num_qubits
Expand Down Expand Up @@ -72,4 +72,15 @@ def _json_dict_(self) -> Dict[str, Any]:
)

def _value_equality_values_(self):
return (self.gate_module, self.gate_name, self._num_qubits, self.gate_args)
hashable = True
for arg in self.gate_args.values():
try:
hash(arg)
except TypeError:
hashable = False
return (
self.gate_module,
self.gate_name,
self._num_qubits,
frozenset(self.gate_args.items()) if hashable else self.gate_args,
)
26 changes: 25 additions & 1 deletion cirq-google/cirq_google/ops/internal_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import cirq
import cirq_google
import pytest


def test_internal_gate():
Expand All @@ -39,7 +40,30 @@ def test_internal_gate_with_no_args():
g = cirq_google.InternalGate(gate_name="GateWithNoArgs", gate_module='test', num_qubits=3)
assert str(g) == 'test.GateWithNoArgs()'
want_repr = (
"cirq_google.InternalGate(gate_name='GateWithNoArgs', " "gate_module='test', num_qubits=3)"
"cirq_google.InternalGate(gate_name='GateWithNoArgs', gate_module='test', num_qubits=3)"
)
assert repr(g) == want_repr
assert cirq.qid_shape(g) == (2, 2, 2)


def test_internal_gate_with_hashable_args_is_hashable():
hashable = cirq_google.InternalGate(
gate_name="GateWithHashableArgs",
gate_module='test',
num_qubits=3,
foo=1,
bar="2",
baz=(("a", 1),),
)
_ = hash(hashable)

unhashable = cirq_google.InternalGate(
gate_name="GateWithHashableArgs",
gate_module='test',
num_qubits=3,
foo=1,
bar="2",
baz={"a": 1},
)
with pytest.raises(TypeError, match="unhashable"):
_ = hash(unhashable)

0 comments on commit f4f3b22

Please sign in to comment.