Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-python/quantum/superdense_coding.py at master · zinating/algorithms-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
zinating
/
algorithms-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
algorithms-python
/
quantum
/
superdense_coding.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
102 lines (87 loc) · 3.3 KB
master
Breadcrumbs
algorithms-python
/
quantum
/
superdense_coding.py
Copy path
Top
File metadata and controls
Code
Blame
102 lines (87 loc) · 3.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Build the superdense coding protocol. This quantum
circuit can send two classical bits using one quantum
bit. This circuit is designed using the Qiskit
framework. This experiment run in IBM Q simulator
with 1000 shots.
.
References:
https://qiskit.org/textbook/ch-algorithms/superdense-coding.html
https://en.wikipedia.org/wiki/Superdense_coding
"""
import
math
import
qiskit
from
qiskit
import
Aer
,
ClassicalRegister
,
QuantumCircuit
,
QuantumRegister
,
execute
def
superdense_coding
(
bit_1
:
int
=
1
,
bit_2
:
int
=
1
)
->
qiskit
.
result
.
counts
.
Counts
:
"""
The input refer to the classical message
that you wants to send. {'00','01','10','11'}
result for default values: {11: 1000}
┌───┐ ┌───┐
qr_0: ─────┤ X ├──────────┤ X ├─────
┌───┐└─┬─┘┌───┐┌───┐└─┬─┘┌───┐
qr_1: ┤ H ├──■──┤ X ├┤ Z ├──■──┤ H ├
└───┘ └───┘└───┘ └───┘
cr: 2/══════════════════════════════
Args:
bit_1: bit 1 of classical information to send.
bit_2: bit 2 of classical information to send.
Returns:
qiskit.result.counts.Counts: counts of send state.
>>> superdense_coding(0,0)
{'00': 1000}
>>> superdense_coding(0,1)
{'01': 1000}
>>> superdense_coding(-1,0)
Traceback (most recent call last):
...
ValueError: inputs must be positive.
>>> superdense_coding(1,'j')
Traceback (most recent call last):
...
TypeError: inputs must be integers.
>>> superdense_coding(1,0.5)
Traceback (most recent call last):
...
ValueError: inputs must be exact integers.
>>> superdense_coding(2,1)
Traceback (most recent call last):
...
ValueError: inputs must be less or equal to 1.
"""
if
(
type
(
bit_1
)
==
str
)
or
(
type
(
bit_2
)
==
str
):
raise
TypeError
(
"inputs must be integers."
)
if
(
bit_1
<
0
)
or
(
bit_2
<
0
):
raise
ValueError
(
"inputs must be positive."
)
if
(
math
.
floor
(
bit_1
)
!=
bit_1
)
or
(
math
.
floor
(
bit_2
)
!=
bit_2
):
raise
ValueError
(
"inputs must be exact integers."
)
if
(
bit_1
>
1
)
or
(
bit_2
>
1
):
raise
ValueError
(
"inputs must be less or equal to 1."
)
# build registers
qr
=
QuantumRegister
(
2
,
"qr"
)
cr
=
ClassicalRegister
(
2
,
"cr"
)
quantum_circuit
=
QuantumCircuit
(
qr
,
cr
)
# entanglement the qubits
quantum_circuit
.
h
(
1
)
quantum_circuit
.
cx
(
1
,
0
)
# send the information
c_information
=
str
(
bit_1
)
+
str
(
bit_2
)
if
c_information
==
"11"
:
quantum_circuit
.
x
(
1
)
quantum_circuit
.
z
(
1
)
elif
c_information
==
"10"
:
quantum_circuit
.
z
(
1
)
elif
c_information
==
"01"
:
quantum_circuit
.
x
(
1
)
else
:
quantum_circuit
.
i
(
1
)
# unentangled the circuit
quantum_circuit
.
cx
(
1
,
0
)
quantum_circuit
.
h
(
1
)
# measure the circuit
quantum_circuit
.
measure
(
qr
,
cr
)
backend
=
Aer
.
get_backend
(
"aer_simulator"
)
job
=
execute
(
quantum_circuit
,
backend
,
shots
=
1000
)
return
job
.
result
().
get_counts
(
quantum_circuit
)
if
__name__
==
"__main__"
:
print
(
f"Counts for classical state send:
{
superdense_coding
(
1
,
1
)
}
"
)
You can’t perform that action at this time.