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

Shorter controlled-hadamard definition #2837

Merged
merged 8 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ The format is based on [Keep a Changelog].
using the option ``with_layout=False`` in the method
``QuantumCircuit.draw``. (\#2739)
- Q-sphere visualization is enhanced and corrected (\#2932)
- Shorter CH gate definition involving only 1 CX (\#2837)
- Now PassManager.draw() (without any argument) will return an in-memory PIL image.


### Removed
- The ability to set the `Timeslot`s for a pulse `Instruction` at initialization.
- The previously deprecated functions
Expand Down
40 changes: 21 additions & 19 deletions qiskit/extensions/standard/ch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"""
controlled-H gate.
"""
import numpy as np

from qiskit.circuit import Gate
from qiskit.circuit import QuantumCircuit
from qiskit.circuit import QuantumRegister
from qiskit.extensions.standard.x import XGate
from qiskit.extensions.standard.h import HGate
from qiskit.extensions.standard.cx import CnotGate
from qiskit.extensions.standard.t import TGate
from qiskit.extensions.standard.t import TdgGate
from qiskit.extensions.standard.s import SGate
from qiskit.extensions.standard.s import SdgGate

Expand All @@ -38,32 +40,25 @@ def __init__(self):
def _define(self):
"""
gate ch a,b {
h b;
sdg b;
cx a,b;
h b;
t b;
cx a,b;
t b;
h b;
s b;
x b;
s a;}
s b;
h b;
t b;
cx a, b;
tdg b;
h b;
sdg b;
}
"""
definition = []
q = QuantumRegister(2, "q")
rule = [
(HGate(), [q[1]], []),
(SdgGate(), [q[1]], []),
(CnotGate(), [q[0], q[1]], []),
(SGate(), [q[1]], []),
(HGate(), [q[1]], []),
(TGate(), [q[1]], []),
(CnotGate(), [q[0], q[1]], []),
(TGate(), [q[1]], []),
(TdgGate(), [q[1]], []),
(HGate(), [q[1]], []),
(SGate(), [q[1]], []),
(XGate(), [q[1]], []),
(SGate(), [q[0]], [])
(SdgGate(), [q[1]], [])
]
for inst in rule:
definition.append(inst)
Expand All @@ -73,6 +68,13 @@ def inverse(self):
"""Invert this gate."""
return CHGate() # self-inverse

def to_matrix(self):
"""Return a Numpy.array for the Ch gate."""
return np.array([[1, 0, 0, 0],
[0, 1/np.sqrt(2), 0, 1/np.sqrt(2)],
[0, 0, 1, 0],
[0, 1/np.sqrt(2), 0, -1/np.sqrt(2)]], dtype=complex)

Copy link
Member

Choose a reason for hiding this comment

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

We should verify the indexing here, and make sure to get enough precision to pass e.g. np.isclose.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kdk I fixed this and added a test. Thanks for the good catch.


def ch(self, ctl, tgt):
"""Apply CH from ctl to tgt."""
Expand Down
33 changes: 33 additions & 0 deletions test/python/circuit/test_gate_definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.


"""Test hardcoded decomposition rules and matrix definitions for standard gates."""

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.test import QiskitTestCase


class TestGateDefinitions(QiskitTestCase):
"""Test the decomposition of a gate in terms of other gates
yields the same matrix as the hardcoded matrix definition."""

def test_ch_definition(self): # TODO: expand this to all gates
"""Test ch gate matrix and definition.
"""
circ = QuantumCircuit(2)
circ.ch(0, 1)
decomposed_circ = circ.decompose()
self.assertTrue(Operator(circ).equiv(Operator(decomposed_circ)))
10 changes: 3 additions & 7 deletions test/python/transpiler/test_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,13 @@ def test_unroll_all_instructions(self):
ref_circuit.cx(qr[0], qr[1])
ref_circuit.u3(0, 0, pi/4, qr[2])
ref_circuit.u3(pi/2, 0, pi, qr[2])
ref_circuit.u3(pi/2, 0, pi, qr[2])
ref_circuit.u3(0, 0, -pi/2, qr[2])
ref_circuit.cx(qr[0], qr[2])
ref_circuit.u3(0, 0, pi/2, qr[2])
ref_circuit.u3(pi/2, 0, pi, qr[2])
ref_circuit.u3(0, 0, pi/4, qr[2])
ref_circuit.cx(qr[0], qr[2])
ref_circuit.u3(0, 0, pi/2, qr[0])
ref_circuit.u3(0, 0, pi/4, qr[2])
ref_circuit.u3(0, 0, -pi/4, qr[2])
ref_circuit.u3(pi/2, 0, pi, qr[2])
ref_circuit.u3(0, 0, pi/2, qr[2])
ref_circuit.u3(pi, 0, pi, qr[2])
ref_circuit.u3(0, 0, -pi/2, qr[2])
ref_circuit.u3(0, 0, 0.25, qr[2])
ref_circuit.cx(qr[1], qr[2])
ref_circuit.u3(0, 0, -0.25, qr[2])
Expand Down