From 28e39be00067dc3018125e6abb7ab1c46c268a6f Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Sun, 21 Jul 2019 23:37:57 -0400 Subject: [PATCH 1/6] shorter CH gate definition --- qiskit/extensions/standard/ch.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/qiskit/extensions/standard/ch.py b/qiskit/extensions/standard/ch.py index 8be916181920..14de6cda9ef4 100644 --- a/qiskit/extensions/standard/ch.py +++ b/qiskit/extensions/standard/ch.py @@ -38,32 +38,25 @@ def __init__(self): def _define(self): """ gate ch a,b { - h b; - sdg b; - cx a,b; + s b; h b; t b; - cx a,b; - t b; + cx a, b; + tdg b; h b; - s b; - x b; - s a;} + 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) From 5da7023c88b6ec722884e4447794c1ffe68f8a62 Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Sun, 21 Jul 2019 23:38:59 -0400 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f3ed7091464..75e39f76d9c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,7 +107,7 @@ 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) ### Removed - The ability to set the `Timeslot`s for a pulse `Instruction` at initialization. From 9e2ecf5bad24bbe8f26d92104e6acfb07a9af0d3 Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Sun, 21 Jul 2019 23:58:26 -0400 Subject: [PATCH 3/6] add ch matrix --- qiskit/extensions/standard/ch.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/qiskit/extensions/standard/ch.py b/qiskit/extensions/standard/ch.py index 14de6cda9ef4..a655ff379a78 100644 --- a/qiskit/extensions/standard/ch.py +++ b/qiskit/extensions/standard/ch.py @@ -20,10 +20,10 @@ 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 @@ -38,13 +38,13 @@ def __init__(self): def _define(self): """ gate ch a,b { - s b; - h b; - t b; - cx a, b; - tdg b; - h b; - sdg b; + s b; + h b; + t b; + cx a, b; + tdg b; + h b; + sdg b; } """ definition = [] @@ -66,6 +66,13 @@ def inverse(self): """Invert this gate.""" return CHGate() # self-inverse + def to_matrix(self): + """Return a Numpy.array for the Ch gate.""" + return numpy.array([[1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 0.707, 0.707], + [0, 0, 0.707, -0.707]], dtype=complex) + def ch(self, ctl, tgt): """Apply CH from ctl to tgt.""" From 19488fdb332748c41206f4c9b8f270fa053e9d7f Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Mon, 22 Jul 2019 09:59:34 -0400 Subject: [PATCH 4/6] import numpy --- qiskit/extensions/standard/ch.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qiskit/extensions/standard/ch.py b/qiskit/extensions/standard/ch.py index a655ff379a78..7f3da99fb4b6 100644 --- a/qiskit/extensions/standard/ch.py +++ b/qiskit/extensions/standard/ch.py @@ -17,6 +17,8 @@ """ controlled-H gate. """ +import numpy + from qiskit.circuit import Gate from qiskit.circuit import QuantumCircuit from qiskit.circuit import QuantumRegister From d7091024d170c68962cc886203b8361d87106e59 Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Mon, 12 Aug 2019 09:51:32 -0400 Subject: [PATCH 5/6] add test --- qiskit/extensions/standard/ch.py | 10 +++--- test/python/circuit/test_gate_definitions.py | 33 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/python/circuit/test_gate_definitions.py diff --git a/qiskit/extensions/standard/ch.py b/qiskit/extensions/standard/ch.py index 7f3da99fb4b6..19c255bc154d 100644 --- a/qiskit/extensions/standard/ch.py +++ b/qiskit/extensions/standard/ch.py @@ -17,7 +17,7 @@ """ controlled-H gate. """ -import numpy +import numpy as np from qiskit.circuit import Gate from qiskit.circuit import QuantumCircuit @@ -70,10 +70,10 @@ def inverse(self): def to_matrix(self): """Return a Numpy.array for the Ch gate.""" - return numpy.array([[1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 0.707, 0.707], - [0, 0, 0.707, -0.707]], dtype=complex) + 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) def ch(self, ctl, tgt): diff --git a/test/python/circuit/test_gate_definitions.py b/test/python/circuit/test_gate_definitions.py new file mode 100644 index 000000000000..122e0d98fa44 --- /dev/null +++ b/test/python/circuit/test_gate_definitions.py @@ -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))) From 527339e907318e3f587c925eee5b591e58f84e3b Mon Sep 17 00:00:00 2001 From: Ali Javadi Date: Thu, 15 Aug 2019 20:48:43 -0400 Subject: [PATCH 6/6] fix broken test --- test/python/transpiler/test_unroller.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/python/transpiler/test_unroller.py b/test/python/transpiler/test_unroller.py index 68ce015a4b1c..2f90ab6b08ba 100644 --- a/test/python/transpiler/test_unroller.py +++ b/test/python/transpiler/test_unroller.py @@ -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])