Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.11k stars 2.34k forks source link

Unexpected global phase on single qubit gates combined using optimize-1q-gates #3083

Closed RonMeiburg closed 3 years ago

RonMeiburg commented 5 years ago

Informations

What is the current behavior?

Simple sequence of single qubit operators XZH results in incorrect overall unitary(example attached): [[ 0.707+0.j -0.707+0.j] [-0.707+0.j -0.707+0.j]]

ZH is correctly represented as: [[ 0.707+0.j 0.707+0.j] [-0.707+0.j 0.707+0.j]]

and X correctly as: [[ 0+0.j 1+0.j] [1+0.j -0+0.j]]

Calculation by hand shows X.ZH to be incorrect, and also inconsistent with qiskit 0.8.2

Steps to reproduce the problem

This is best illustrated by running the following:

print(qiskit.version) q = QuantumRegister(1) c = ClassicalRegister(1) circuit = QuantumCircuit(q, c) circuit.h(q[0]) circuit.z(q[0]) circuit.x(q[0]) job = execute(circuit, backend) result = job.result() print(np.around(result.get_unitary(circuit), 3))

Output: 0.9.0 [[ 0.707+0.j -0.707+0.j] [-0.707+0.j -0.707+0.j]]

What is the expected behavior?

The correct result should be (compare 0.8.2 output below): 0.8.2 [[-0.707+0.j 0.707+0.j] [ 0.707+0.j 0.707+0.j]]

Suggested solutions

Revert to 0.8.2 for relevant code, or compare for the introduction of above behaviour

qiskit_090

qiskit_082

atilag commented 5 years ago

Are you trying with the latest Qiskit release installed via pip? (0.9) Just to make sure everything is fixed now.

atilag commented 5 years ago

Ok, seems like you are using the latest version so I guess that this might be fixed from 0.8.2. I'm closing this. Thanks for reporting!

RonMeiburg commented 5 years ago

No, this was referring to 0.9.0. This is an open issue

chriseclectic commented 5 years ago

I can't reproduce this. On latest stable I get the correct output with your example:

from qiskit import *
from qiskit.quantum_info import *
import numpy as np

print('Terra:', qiskit.__version__)
print('Aer: ', qiskit.providers.aer.__version__)

backend = Aer.get_backend('unitary_simulator')
q = QuantumRegister(1)
c = ClassicalRegister(1)
circuit = QuantumCircuit(q, c)
circuit.h(q[0])
circuit.z(q[0])
circuit.x(q[0])
job = execute(circuit, backend)
result = job.result()

H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
X = np.array([[0, 1], [1, 0]])
Z = np.diag([1, -1])

ref = Operator(X.dot(Z).dot(H))
op1 = Operator(result.get_unitary(circuit))
op2 = Operator(circuit)
print(op1 == ref and op2 == ref)

Returns True for me on Terra 0.9.0, Aer 0.3.0

dudalev commented 5 years ago

The problem is reproducible with BasicAer - https://colab.research.google.com/drive/1BVSV_KnVppRk5N-oXWov5R1tWl9DMB-R

For qiskit version = 0.8.2 , BasicAer result equals Aer result

RonMeiburg commented 5 years ago

Correct. I had subsequently explained this to Juan Gomez upon his questions. That answer appears lost in the thread.

Ron Meiburg

On 9-9-2019 19:53, Michael wrote:

The problem is reproducible with BasicAer - https://colab.research.google.com/drive/1BVSV_KnVppRk5N-oXWov5R1tWl9DMB-R

For qiskit version = 0.8.2 , BasicAer result equals Aer result

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Qiskit/qiskit-aer/issues/335?email_source=notifications&email_token=AMTDP3DUHL7A2NCAC374XZ3QI2EQXA5CNFSM4ISUPWWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6IPKQA#issuecomment-529593664, or mute the thread https://github.com/notifications/unsubscribe-auth/AMTDP3DO3FHC3WM3TAPBFRTQI2EQXANCNFSM4ISUPWWA.

chriseclectic commented 5 years ago

It looks like there is a phase issue in the Terra transpiler pass that combines single-qubit gates. You can see this running on Aer or BasicAer if you manually specify the unroll gates and optimization level.

Using the previous example the following returns the correct result with either Aer or BasicAer

job = execute(circuit, backend, basis_gates=['u1','u2','u3'], optimization_level=0)

The following returns an result with -1 global phase

job = execute(circuit, backend, basis_gates=['u1','u2','u3'], optimization_level=1)
1ucian0 commented 4 years ago

The problem is reproducible with BasicAer - https://colab.research.google.com/drive/1BVSV_KnVppRk5N-oXWov5R1tWl9DMB-R For qiskit version = 0.8.2 , BasicAer result equals Aer result

Moving the example here, just in case:

In [1]: from qiskit import Aer, BasicAer, QuantumCircuit, QuantumRegister, ClassicalRegister, execute 
   ...:  
   ...: q = QuantumRegister(1) 
   ...: c = ClassicalRegister(1) 
   ...:  
   ...: circuit = QuantumCircuit(q, c) 
   ...:  
   ...: circuit.h(q[0]) 
   ...: circuit.z(q[0]) 
   ...: circuit.x(q[0]) 
   ...:  
   ...: aer = BasicAer 
   ...:  
   ...: backend = aer.get_backend('unitary_simulator') 
   ...: job = execute(circuit, backend) 
   ...: result = job.result() 
   ...: result.get_unitary(circuit)                                                                                                          
Out[1]: 
array([[ 0.70710678+0.j, -0.70710678+0.j],
       [-0.70710678+0.j, -0.70710678+0.j]])
adjs commented 4 years ago

PR #4036 fix this issue. @ajavadia, I just saw PR #3658 after finishing the fix.

ewinston commented 3 years ago

@1ucian0 Has this been resolved? Your example seems to produce the correct response as of 0.18.0.dev0+31a24c1

1ucian0 commented 3 years ago

Indeed, it seems fixed. Adding the test from #4036 in #6295 for closing the loop.