Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of multiplication of cliffords #1275

Merged
merged 18 commits into from
Sep 26, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ def compose_2q(lhs: Integral, rhs: Integral) -> Integral:
"""Return the composition of 2-qubit clifford integers."""
num = lhs
for layer, idx in enumerate(_layer_indices_from_num(rhs)):
circ = _CLIFFORD_LAYER[layer][idx]
num = _compose_num_with_circuit_2q(num, circ)
gate_numbers = _CLIFFORD_LAYER_NUMS[layer][idx]
for n in gate_numbers:
num = _CLIFFORD_COMPOSE_2Q_DENSE[num, _clifford_num_to_dense[n]]
return num


Expand Down Expand Up @@ -569,6 +570,20 @@ def _create_cliff_2q_layer_2():
_NUM_LAYER_1 = 20
_NUM_LAYER_2 = 16

# Construct a dense multiplication table
itoko marked this conversation as resolved.
Show resolved Hide resolved
_CLIFFORD_LAYER_NUMS=[]
for layer in [0,1,2]:
_CLIFFORD_LAYER_NUMS.append([])
for idx, qc in enumerate(_CLIFFORD_LAYER[layer]):
nn = []
for inst in qc:
qubits = tuple(qc.find_bit(q).index for q in inst.qubits)
rhs = _num_from_2q_gate(op=inst.operation, qubits=qubits)
nn.append(rhs)
_CLIFFORD_LAYER_NUMS[layer].append(tuple(nn))
eendebakpt marked this conversation as resolved.
Show resolved Hide resolved
_valid_indices=np.unique(list(itertools.chain(*itertools.chain(*_CLIFFORD_LAYER_NUMS))))
_clifford_num_to_dense = { idx: ii for ii, idx in enumerate(_valid_indices)}
_CLIFFORD_COMPOSE_2Q_DENSE = (_CLIFFORD_COMPOSE_2Q[:, _valid_indices]).toarray()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my memo. I'm surprised to know the custom dense matrix is faster than scipy.lil_matrix. If we used lil_matrix, the benchmark takes 18.76 sec, so about half of the improvement comes from the use of the custom dense matrix.


@lru_cache(maxsize=None)
def _transformed_clifford_layer(
Expand Down