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

Approximate quantum compiler (AQC) returns a wrong circuit when the unitary has a determinant −1 #9327

Closed
EgrettaThula opened this issue Dec 28, 2022 · 2 comments · Fixed by #9331
Labels
bug Something isn't working

Comments

@EgrettaThula
Copy link
Contributor

Environment

  • Qiskit Terra version: 0.22.3
  • Python version: 3.9.5
  • Operating system: Windows 10

What is happening?

When the approximate quantum compiler (AQC) is used to synthesis a unitary which has a determinant −1 (e.g., an "odd parity" permutation matrix), a warning message is displayed:

[...]synthesis\aqc\aqc.py:83: RuntimeWarning: invalid value encountered in power
  su_matrix = target_matrix / np.power(target_det, (1 / matrix_dim))

then it continues its work and generates a circuit. The unitary of the generated circuit, however, is not an approximation to the original one.

How can we reproduce the issue?

unitary = np.ndarray(shape=(8, 8), buffer=np.array([
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0],
    [1, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 1],
], dtype=int), dtype=int)

num_qubits = 3

cnots = make_cnot_network(num_qubits = num_qubits, network_layout = 'spin', connectivity_type = 'full', depth = 0)

optimizer = L_BFGS_B()
aqc = AQC(optimizer)
approximate_circuit = CNOTUnitCircuit(num_qubits=num_qubits, cnots=cnots)
approximating_objective = DefaultCNOTUnitObjective(num_qubits=num_qubits, cnots=cnots)

aqc.compile_unitary(
    target_matrix=unitary,
    approximate_circuit=approximate_circuit,
    approximating_objective=approximating_objective,
)

approx_matrix = Operator(approximate_circuit).data
error = 0.5 * (np.linalg.norm(approx_matrix - unitary, "fro") ** 2)

print(error)
array_to_latex(approx_matrix)

What should happen?

The code must take this case into account

Any suggestions?

The root cause of this wrong behavior is this line of code:

su_matrix = target_matrix / np.power(target_det, (1 / matrix_dim))

As per numpy.power documentation:

Negative values raised to a non-integral value will return nan. To get complex results, cast the input to complex.

The issue can be simply fixed by replacing this line with:

su_matrix = target_matrix / np.power(target_det, (1 / matrix_dim), dtype=complex)
@EgrettaThula EgrettaThula added the bug Something isn't working label Dec 28, 2022
@ajavadia
Copy link
Member

Thanks @EgrettaThula for finding this bug, would you like to make a PR to implement your suggestion? You would just have to make the change you suggest, and add a test case.

@EgrettaThula
Copy link
Contributor Author

I'll work on it.

@mergify mergify bot closed this as completed in #9331 Jan 13, 2023
mergify bot added a commit that referenced this issue Jan 13, 2023
#9331)

* Fix: AQC returns a wrong circuit when the unitary has a determinant −1 (#9327)

* Added release notes

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants