-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implements optimizations A.1 and A.2 for Quantum Shannon Decomposition #7390
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
Open
codrut3
wants to merge
6
commits into
quantumlib:main
Choose a base branch
from
codrut3:issue-6777
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NoureldinYosri Please have a look! |
For future reference, this is the script I used to benchmark the decomposition: # pylint: disable=wrong-or-nonexistent-copyright-notice
"""Tests performance of quantum Shannon decomposition.
"""
import sys
import time
from scipy.stats import unitary_group
import cirq
import qiskit.qasm2
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
def main():
for n_qubits in range(3, 9):
U = unitary_group.rvs(2**n_qubits)
qubits = [cirq.NamedQubit(f'q{i}') for i in range(n_qubits)]
start_time = time.time()
circuit = cirq.Circuit(cirq.quantum_shannon_decomposition(qubits, U))
elapsed_time = time.time() - start_time
print(f'Depth for {n_qubits} qubits (no transpiler): {len(circuit)} ({elapsed_time:.2f} sec)')
print(f'Number of CZ gates (no transpiler): {len(list(circuit.findall_operations_with_gate_type(cirq.CZPowGate)))}')
print(f'Number of CNOT gates (no transpiler): {len(list(circuit.findall_operations_with_gate_type(cirq.CXPowGate)))}')
# Export to qasm
qasm_str = cirq.qasm(circuit, args=cirq.QasmArgs(version="2.0"))
qiskit_circuit = qiskit.qasm2.loads(qasm_str, include_path=('.',), custom_instructions=qiskit.qasm2.LEGACY_CUSTOM_INSTRUCTIONS, custom_classical=(), strict=False)
# Run qiskit transpiler
start_time = time.time()
opt_circuit = qiskit.compiler.transpile(qiskit_circuit, basis_gates=["cz", "u3"], optimization_level=3)
elapsed_time = time.time() - start_time
print(f'Qiskit depth for {n_qubits} qubits (with transpiler): {opt_circuit.depth()} ({elapsed_time:.2f} sec)')
print(f'Qiskit gate counts for {n_qubits} qubits (with transpiler): {dict(opt_circuit.count_ops())}')
# Run cirq transpiler
start_time = time.time()
circuit = cirq.optimize_for_target_gateset(circuit, gateset=cirq.CZTargetGateset(preserve_moment_structure=False), max_num_passes=None)
elapsed_time = time.time() - start_time
print(f'Cirq depth for {n_qubits} qubits (with transpiler): {len(circuit)} ({elapsed_time:.2f} sec)')
# Count the operations
operation_counts = {}
for op in circuit.all_operations():
gate_type = op.gate.__class__
if isinstance(op.gate, cirq.CZPowGate):
gate_type = 'cz'
elif isinstance(op.gate, cirq.PhasedXZGate):
gate_type = 'u3'
if gate_type not in operation_counts:
operation_counts[gate_type] = 0
operation_counts[gate_type] += 1
print(f'Cirq gate counts for {n_qubits} qubits (with transpiler): {operation_counts}')
if __name__ == '__main__':
main() |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7390 +/- ##
=======================================
Coverage 98.68% 98.68%
=======================================
Files 1112 1112
Lines 97642 97703 +61
=======================================
+ Hits 96359 96421 +62
+ Misses 1283 1282 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #6777
I checked that with the current PR, the circuit depth after transpiler matches that obtained by Qiskit.
The runtime is slow due
merge_single_qubit_gates_to_phased_x_and_z
. I wrote a different implementation for the special case used by the Shannon decomposition. With this, the runtime is halved. I believemerge_single_qubit_gates_to_phased_x_and_z
can be optimized too.